按下按钮时我的界面冻结了 . 我正在使用线程,但我不确定为什么仍然悬挂 . 任何帮助将不胜感激 . 提前致谢
class magic:
def __init__(self):
self.mainQueue=queue.Queue()
def addItem(self,q):
self.mainQueue.put(q)
def startConverting(self,funcName):
if(funcName=="test"):
while not self.mainQueue.empty():
t = Thread(target = self.threaded_function)
t.start()
t.join()
def threaded_function(self):
time.sleep(5)
print(self.mainQueue.get())
m=magic()
def helloCallBack():
m.addItem("asd")
m.startConverting("test") //this line of code is freezing
B = tkinter.Button(top, text ="Hello", command = helloCallBack)
B.pack()
top.mainloop()
1 回答
这是使用基于tkinter的GUI执行异步任务的方法 . 我根据引用书中的食谱改编了它 . 您应该能够修改它以满足您的需要 .
为了保持GUI的响应性,不要通过执行类似_28395的后台线程来干扰它的
mainloop()
,这会使GUI "hang"直到线程完成 . 这是通过使用通用after()窗口小部件方法定期轮询Queue
来完成的 .