【发布时间】:2019-07-03 08:48:14
【问题描述】:
我正在用树莓派做一些测量。我用 python tkinter 做了一个 GUI。每秒钟进行一次测量。当测量值高于特定值时,我想将某个引脚设置为高电平 30 秒(计数器)。但应继续测量。在此期间,如果有另一个高值,再次将计数器重置为 30 秒。所以我使用了一个while循环。我在 GUI 中显示测量值。我使用 .after 方法每秒钟调用一次主函数(readSensor)。在 while 循环条件下,GUI 被冻结,屏幕上的值没有更新,按钮也不起作用。我应该删除while循环并使用.after吗?但是那里的柜台怎么用呢?
status = False
def start():
global status
status = True
def stop():
global status
status = False
def readSensor():
# there is a start and stop button in GUI
if status:
measuredValues = []
pi.write(23,0) # pin 23 is low now
# code to do measurement is here
#it works fine
#result is a list with name measuredValues
#then checking the conditions below
if((measuredValues[0] > Limit_1) or (measuredValues[1] > Limit_2) or (measuredValues[2] > Limit_3) or (measuredValues[3] > Limit_4)):
counter = 0
print(measuredValues)
while (counter <=30):
# this while is the problematic part. if condition is true, i will set the pin 23 high.
# i would like to set the pin high for 30 seconds
# no any buttons seems working during this time. why?
# stop button is also not working.
pi.write(23,1)
time.sleep(1.0) #shall i avoid this sleep?
print(counter)
measuredValues = []
#measurement will continue here
#same code to do measurement as before
#output is the same list with name measuredValues
print(measuredValues)
# if any of the new measuring values is/are higher than limit, want to reset the counter to 30
# shall I use .after method here? and avoid while loop?
# how to use it with a condition like maiximum 30s
if ((measuredValues[0] > Limit_1) or (measuredValues[1] > Limit_2) or (measuredValues[2] > Limit_3) or (measuredValues[3] > Limit_4) ):
counter = 0
else:
counter +=1
else:
pi.write(23,0)
print(measuredValues)
win.after(1000, readSensor) # new measurement every second
win = Tk()
# code for GUI here
win.after(1000, readSensor)
win.mainloop()
【问题讨论】:
-
如果不推荐在
tkinter mainloop中使用while loop和time.sleep,这就是您的GUI 变得无响应的原因。 -
@ReblochonMasque 我应该去穿线吗?有什么建议吗?
标签: python user-interface tkinter