更新线程 tkinter gui
update threaded tkinter gui
我有一个小显示器连接到我的 pi。
现在我有一个 Python 脚本来测量 gpio headers 的两个事件之间的时间。
我想显示这个时间(获取这个时间的脚本运行良好)。为此,我创建了一个 tkinter
window。
在那里,我有一个这次应该显示的标签。
我已经线程化了 gui 函数,使程序仍然可以监听 GPIO 引脚。
def guiFunc():
gui = Tk()
gui.title("Test")
gui.geometry("500x200")
app = Frame(gui)
app.grid()
beattime = Label(app, text = "test")
beattime.grid()
gui.mainloop()
gui_thread = threading.Thread(target = guiFunc)
gui_thread.start()
while True:
time.sleep(.01)
if (GPIO.input(3)):
time = trigger() #trigger is the function to trigger the 'stopwatch'
global beattime
beattime['text'] = str(time)
while GPIO.input(3): #'wait' for btn to release (is there a better way?)
print "btn_pressed"
所以自从我添加了这些行之后程序没有做任何事情:
global beattime
beattime['text'] = str(time)
我做错了什么?
使用tkinter.StringVar
# omitting lines
global timevar
timevar = StringVar()
timevar.set("Test")
beattime = Label(app, textvariable=timevar)
# omitting lines
#changing the text:
while True:
time.sleep(.01)
if (GPIO.input(3)):
time = trigger() #trigger is the function to trigger the 'stopwatch'
timevar.set(str(time))
root.update() #just in case
while GPIO.input(3): #'wait' for btn to release (is there a better way?)
print "btn_pressed"
并且您应该 运行 主线程中的 gui。不建议从不同线程调用gui调用。
我有一个小显示器连接到我的 pi。
现在我有一个 Python 脚本来测量 gpio headers 的两个事件之间的时间。
我想显示这个时间(获取这个时间的脚本运行良好)。为此,我创建了一个 tkinter
window。
在那里,我有一个这次应该显示的标签。
我已经线程化了 gui 函数,使程序仍然可以监听 GPIO 引脚。
def guiFunc():
gui = Tk()
gui.title("Test")
gui.geometry("500x200")
app = Frame(gui)
app.grid()
beattime = Label(app, text = "test")
beattime.grid()
gui.mainloop()
gui_thread = threading.Thread(target = guiFunc)
gui_thread.start()
while True:
time.sleep(.01)
if (GPIO.input(3)):
time = trigger() #trigger is the function to trigger the 'stopwatch'
global beattime
beattime['text'] = str(time)
while GPIO.input(3): #'wait' for btn to release (is there a better way?)
print "btn_pressed"
所以自从我添加了这些行之后程序没有做任何事情:
global beattime
beattime['text'] = str(time)
我做错了什么?
使用tkinter.StringVar
# omitting lines
global timevar
timevar = StringVar()
timevar.set("Test")
beattime = Label(app, textvariable=timevar)
# omitting lines
#changing the text:
while True:
time.sleep(.01)
if (GPIO.input(3)):
time = trigger() #trigger is the function to trigger the 'stopwatch'
timevar.set(str(time))
root.update() #just in case
while GPIO.input(3): #'wait' for btn to release (is there a better way?)
print "btn_pressed"
并且您应该 运行 主线程中的 gui。不建议从不同线程调用gui调用。