我有一个Python脚本,它使用Tkinter作为GUI.我的小脚本应该每隔X秒创建一个Toplevel小部件.当我运行我的代码时,第一个Toplevel小部件成功创建,但当它尝试创建第二个时,程序崩溃.
我正在做的是使用after方法每隔5秒与root的mainloop一起调用函数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()
Run Code Online (Sandbox Code Playgroud)