mainloop中的Python While循环导致延迟
当我使用mainloop中的Python While循环导致延迟,python,loops,user-interface,tkinter,temperature,Python,Loops,User Interface,Tkinter,Temperature,当我使用while True:循环运行代码时,它包含在root.mainloop()中,这会使我的GUI严重滞后,并且代码运行不如我希望的那样平稳。我想知道我如何使我的代码运行平稳,没有滞后 为了这个测试的目的,我已经注释掉了大部分代码,这些代码只有在连接到我的raspberry pi时才起作用 提前感谢您的帮助 import os import glob import time #import RPi.GPIO as GPIO from datetime import datetime fro
while True:
循环运行代码时,它包含在root.mainloop()
中,这会使我的GUI严重滞后,并且代码运行不如我希望的那样平稳。我想知道我如何使我的代码运行平稳,没有滞后
为了这个测试的目的,我已经注释掉了大部分代码,这些代码只有在连接到我的raspberry pi时才起作用
提前感谢您的帮助
import os
import glob
import time
#import RPi.GPIO as GPIO
from datetime import datetime
from tkinter import *
'''
#Set gpio's
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(17,GPIO.OUT)#RED
GPIO.setup(22,GPIO.OUT)#GREEN
GPIO.setup(27,GPIO.OUT)#BLUE
#grab temp probe information
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
# Read temperature from device
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
lines=read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.1)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000
#temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_c#, temp_f
'''
temp = 18
desiredtemp = 17
deg = u'\xb0'#utf code for degree
#increase button press
def increase():
global desiredtemp
desiredtemp += 0.5
tmpstr.set("%s" % desiredtemp)
#Decrease button press
def decrease():
global desiredtemp
desiredtemp -= 0.5
tmpstr.set("%s" % desiredtemp)
#Tkinter start
root = Tk()
root.wm_title("Temp") #Name the title bar
#code to add widgets will go here....
#make 3 frames for text and buttons
topFrame = Frame(root)
topFrame.pack(side=TOP)
middleFrame = Frame(root)
middleFrame.pack()
bottomFrame = Frame(root)
bottomFrame.pack(side=BOTTOM)
tmpstr = StringVar(value="%s" % desiredtemp)
crtmpstr = StringVar(value="%s" % temp)
#Set labels
label1 = Label(topFrame, text="Desired Temp = ", fg="black")
label2 = Label(middleFrame, text="Actual Temp = ", fg="black")
label3 = Label(topFrame, textvariable=tmpstr, fg="black")
label4 = Label(middleFrame, textvariable=crtmpstr, fg="black")
#use to put labels on screen
label1.pack(side=LEFT)
label2.pack(side=LEFT)
label3.pack(side=LEFT)
label4.pack(side=LEFT)
#Set buttons
button1 = Button(bottomFrame, text="Increase (0.5"+ deg +"C)", fg="black", command=increase)
button2 = Button(bottomFrame, text="Decrease (0.5"+ deg +"C)", fg="black", command=decrease)
#use to put buttons on screen
button1.pack(side=LEFT)
button2.pack(side=LEFT)
#Tkinter End
# Open file to be logged
'''
file = open("/home/pi/Desktop/Templog.csv", "a")
if os.stat("/home/pi/Desktop/Templog.csv").st_size == 0:
file.write("Date, Time, TemperatureSensor1\n")
'''
# Continuous print loop
while 1:
print(temp)
if(temp<=desiredtemp):
#GPIO.output(17,GPIO.LOW)
#GPIO.output(22,GPIO.HIGH)
temp += 5
crtmpstr.set("%s" % temp)
else:
#GPIO.output(17,GPIO.HIGH)
#GPIO.output(22,GPIO.LOW)
temp -=0.5
crtmpstr.set("%s" % temp)
#now = datetime.now()
#file.write(str(now.day)+"-"+str(now.month)+"-"+str(now.year)+","+str(now.hour)+":"+str(now.minute)+":"+str(now.second)+","+str(read_temp())+"\n")
#file.flush()
time.sleep(1)
root.update()
root.mainloop()
导入操作系统
导入glob
导入时间
#将RPi.GPIO导入为GPIO
从日期时间导入日期时间
从tkinter进口*
'''
#设置gpio的
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(17,GPIO.OUT)#红色
GPIO.setup(22,GPIO.OUT)#绿色
GPIO.setup(27,GPIO.OUT)#蓝色
#抓取温度探针信息
操作系统('modprobe w1 gpio')
操作系统('modprobe w1 therm')
基本目录='/sys/bus/w1/devices/'
device_folder=glob.glob(基本目录+28*)[0]
设备文件=设备文件夹+'/w1\U从机'
#从设备读取温度
def read_temp_raw():
f=打开(设备_文件'r')
行=f.读行()
f、 关闭()
回程线
def read_temp():
行=读取温度原始值()
而行[0].strip()[-3:!='是的:
睡眠时间(0.1)
行=读取温度原始值()
等于\u pos=行[1]。查找('t='))
如果等于_pos!=-1:
临时字符串=行[1][等于位置+2:]
温度c=浮动(温度字符串)/1000
#温度f=温度c*9.0/5.0+32.0
返回温度c,温度f
'''
温度=18
desiredtemp=17
deg=u'\xb0'#度的utf代码
#增加按钮按下次数
def增加():
全球期望时间
所需温度+=0.5
tmpstr.set(“%s”%desiredtemp)
#减少按钮按下
def减少():
全球期望时间
期望温度-=0.5
tmpstr.set(“%s”%desiredtemp)
#特金特起动
root=Tk()
root.wm_title(“Temp”)#命名标题栏
#添加小部件的代码将显示在此处。。。。
#为文本和按钮制作3帧
顶部框架=框架(根)
顶部框架。包装(侧面=顶部)
中间帧=帧(根)
middleFrame.pack()
底部框架=框架(根)
底部框架。包装(侧面=底部)
tmpstr=StringVar(value=“%s”%desiredtemp)
crtmpstr=StringVar(value=“%s”%temp)
#设置标签
标签1=标签(顶框,text=“所需温度=,fg=“黑色”)
label2=标签(中间框,text=“实际温度=,fg=“黑色”)
label3=标签(顶部框架,textvariable=tmpstr,fg=“黑色”)
label4=标签(中间框,textvariable=crtmpstr,fg=“黑色”)
#用于在屏幕上放置标签
标签1.包装(侧面=左侧)
标签2.包装(侧面=左侧)
标签3.包装(侧面=左侧)
标签4.包装(侧面=左侧)
#设置按钮
按钮1=按钮(底部框架,text=“增加(0.5”+摄氏度)”,fg=“黑色”,命令=增加)
按钮2=按钮(底部框架,text=“降低(0.5”+摄氏度)”,fg=“黑色”,命令=降低)
#用于在屏幕上放置按钮
按钮1.包装(侧面=左侧)
按钮2.包装(侧面=左侧)
#特金特端
#打开要记录的文件
'''
文件=打开(“/home/pi/Desktop/Templog.csv”,“a”)
如果os.stat(“/home/pi/Desktop/Templog.csv”).st_size==0:
file.write(“日期、时间、温度传感器1\n”)
'''
#连续打印循环
而1:
打印(临时)
if(temp只需使用TK
对象的after
方法。这不会影响重绘,也不需要调用任何手动更新函数,因为它会延迟该代码的执行,直到gui线程不忙为止
将要独立执行的代码拆分为一个单独的函数,并将其传递给根用户。
之后加上一个时间延迟。我第一次使用延迟0,因此它立即执行。然后在函数结束时再次调用它,这次传递值1000(毫秒)作为延迟。它将重复执行,直到您结束tkinter应用程序
# ... other code here
def gpiotask():
global temp
print(temp)
if(temp <= desiredtemp):
GPIO.output(17, GPIO.LOW)
GPIO.output(22, GPIO.HIGH)
temp += 5 # <- did you mean 0.5 here ?
crtmpstr.set("%s" % temp)
else:
GPIO.output(17, GPIO.HIGH)
GPIO.output(22, GPIO.LOW)
temp -= 0.5
crtmpstr.set("%s" % temp)
root.after(1000, gpiotask)
root.after(0, gpiotask)
root.mainloop()
#…此处还有其他代码
def gpiotask():
全球温度
打印(临时)
如果你看了我之前链接的问题了吗