我有一个使用 Tkinter 作为 GUI 的 Python 脚本。我的小脚本应该每 X 秒创建一个 Toplevel 小部件。当我运行我的代码时,第一个 Toplevel 小部件已成功创建,但当它尝试创建第二个时,程序崩溃了。
我正在做的是使用 after 方法与 root 的主循环一起每 5 秒调用一次函数 startCounting。每次调用此函数时,我都会将一个 Toplevel 小部件对象附加到列表中并启动一个新线程,希望该线程将运行新的主循环。
如果有人能解决这个问题,我将不胜感激。顺便说一句,这只是我目前用来解决我的问题的一个小脚本,它阻止我继续我真正的学校项目。
代码:
import threading,thread
from Tkinter import *
def startCounting():
global root
global topLevelList
global classInstance
topLevelList.append (Toplevel())
topLevelList[len(topLevelList)-1].title("Child")
classInstance.append(mainLoopThread(topLevelList[len(topLevelList)-1]))
root.after(5000,startCounting)
class mainLoopThread(threading.Thread):
def __init__(self,toplevelW):
self.toplevelW = toplevelW
threading.Thread.__init__(self)
self.start()
def run(self):
self.toplevelW.mainloop()
global classInstance
classInstance = []
global topLevelList
topLevelList = []
global root
root = Tk()
root.title("Main")
startCounting()
root.mainloop()
最佳答案
Tkinter 被设计为仅从主线程运行。参见 the docs :
Just run all UI code in the main thread, and let the writers write to a Queue object; e.g.
...接下来是一个实质性示例,显示辅助线程将请求写入队列,主循环专门负责与 Tk 的所有直接交互。
许多对象和子系统不喜欢接收来自多个不同线程的请求,在 GUI 工具包的情况下,通常需要专门只使用 main 线程。
解决这个问题的正确 Python 架构总是将一个线程(主线程,如果必须的话)用于服务挑剔的对象或子系统;需要与所述子系统或对象交互的每个其他线程必须通过将请求排队到专用线程来获得它(如果由于某些请求而需要结果,则可能在“返回队列”上等待结果)。对于通用线程,这也是一个非常可靠的 Python 架构(我在“Python 简述”中详细阐述了它,但那是另一个主题;-)。
关于python - 创建第二个 Toplevel 小部件时线程化 Tkinter 脚本崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3567238/