从队列中获取消息后更新标签
Updating label upon getting message from queue
我正在尝试更新标签以在它通过队列接收数字时显示正在倒数的数字。我能够看到它正在控制台中打印,但标签没有改变。任何帮助或建议都会有所帮助!
这是我的代码:
import tkinter as tk
import time
import threading
import queue
class GUIApp:
def __init__(self):
self.root = tk.Tk()
self.buttonCountDown = tk.Button(text='Count Down', command=self.countDownAction)
self.buttonCountDown.pack()
self.label = tk.Label(text='default')
self.label.pack()
self.queue = queue.Queue()
self.root.mainloop()
def countDown(self, seconds):
for i in range(seconds, 0, -1):
self.queue.put(i)
time.sleep(1)
def listenToQueue(self):
while True:
try:
if self.queue.empty() == False:
print(self.queue.get(0))
self.label['text'] = self.queue.get(0)
elif self.queue.empty() == True:
pass
except queue.Empty:
pass
def countDownAction(self):
listenThread = threading.Thread(target=self.listenToQueue)
listenThread.start()
thread = threading.Thread(target=self.countDown, args=(5,))
thread.start()
thread.join()
app = GUIApp()
您需要知道的第一件事是 Queue.get()
删除项目并 returns 它,类似于 dict.pop()
。因此,当您执行 print(self.queue.get(0))
时,该项目已从队列中删除。如果你想同时打印和配置它,你必须先将它分配给一个变量:
def listenToQueue(self):
while True:
try:
if self.queue.empty() == False:
s = self.queue.get(0)
print (s)
self.label['text'] = s
elif self.queue.empty() == True:
pass
except queue.Empty:
pass
接下来,调用thread.join()
将等待线程终止。在当前设置中,您根本不需要调用此方法。
def countDownAction(self):
listenThread = threading.Thread(target=self.listenToQueue)
listenThread.start()
thread = threading.Thread(target=self.countDown, args=(5,))
thread.start()
#thread.join() #not required
我正在尝试更新标签以在它通过队列接收数字时显示正在倒数的数字。我能够看到它正在控制台中打印,但标签没有改变。任何帮助或建议都会有所帮助!
这是我的代码:
import tkinter as tk
import time
import threading
import queue
class GUIApp:
def __init__(self):
self.root = tk.Tk()
self.buttonCountDown = tk.Button(text='Count Down', command=self.countDownAction)
self.buttonCountDown.pack()
self.label = tk.Label(text='default')
self.label.pack()
self.queue = queue.Queue()
self.root.mainloop()
def countDown(self, seconds):
for i in range(seconds, 0, -1):
self.queue.put(i)
time.sleep(1)
def listenToQueue(self):
while True:
try:
if self.queue.empty() == False:
print(self.queue.get(0))
self.label['text'] = self.queue.get(0)
elif self.queue.empty() == True:
pass
except queue.Empty:
pass
def countDownAction(self):
listenThread = threading.Thread(target=self.listenToQueue)
listenThread.start()
thread = threading.Thread(target=self.countDown, args=(5,))
thread.start()
thread.join()
app = GUIApp()
您需要知道的第一件事是 Queue.get()
删除项目并 returns 它,类似于 dict.pop()
。因此,当您执行 print(self.queue.get(0))
时,该项目已从队列中删除。如果你想同时打印和配置它,你必须先将它分配给一个变量:
def listenToQueue(self):
while True:
try:
if self.queue.empty() == False:
s = self.queue.get(0)
print (s)
self.label['text'] = s
elif self.queue.empty() == True:
pass
except queue.Empty:
pass
接下来,调用thread.join()
将等待线程终止。在当前设置中,您根本不需要调用此方法。
def countDownAction(self):
listenThread = threading.Thread(target=self.listenToQueue)
listenThread.start()
thread = threading.Thread(target=self.countDown, args=(5,))
thread.start()
#thread.join() #not required