【发布时间】:2021-04-20 19:58:14
【问题描述】:
是否可以让 GUI 更新变量 'a' 以在 'thread2' 增加值时实时更新?
import tkinter as tk
from threading import Thread
import time
a = 0 # global variable
def thread1(threadname):
root = tk.Tk()
w = tk.Label(root, text=a)
w.pack()
root.mainloop()
def thread2(threadname):
global a
while True:
a += 1
time.sleep(1)
thread1 = Thread( target=thread1, args=("Thread-1", ) )
thread2 = Thread( target=thread2, args=("Thread-2", ) )
thread1.start()
thread2.start()
如果我创建一个循环并打印“a”,我会得到正确的结果。
def thread1(threadname):
global a
while True:
print(a)
# root = tk.Tk()
# w = tk.Label(root, text=a)
# w.pack()
# root.mainloop()
任何帮助将不胜感激
【问题讨论】:
标签: python multithreading tkinter