创建第二个Toplevel小部件时,Threaded Tkinter脚本崩溃

use*_*825 8 python multithreading tkinter

我有一个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)

Ale*_*lli 24

Tkinter仅设计为从主线程运行.查看文档:

只需在主线程中运行所有UI代码,让编写器写入Queue对象; 例如

...接下来是一个实例,显示了将请求写入队列的辅助线程,主循环专门负责与Tk的所有直接交互.

许多对象和子系统不喜欢接收来自多个不同线程的请求,并且在GUI工具包的情况下,通常只需要使用线程并不罕见.

这个问题的正确Python架构总是用一个线程(主要的,如果必须的话)来服务于挑剔的对象或子系统; 每个需要与所述子系统或对象交互的其他线程必须通过将请求排队到专用线程来获得它(并且如果某些请求需要结果,则可能在结果的"返回队列"上等待).这也是用于通用线程的非常合理的Python架构(我在"果壳中的Python"中详细阐述了它,但这是另一个主题;-).

  • 我遇到了这个限制,这正是我使用的策略.我有一个方法来检查所有队列.然后我通过调用root.after(ms,my_method)在Tkinter的主循环中注册该方法.my_method中的最后一次调用是对root.after的另一次调用,以便my_method不断地使用主循环重新注册.重要的是my_method中的异常不会导致您错过在mainloop中注册.您可能希望在try/finally的finally部分中调用root.after. (9认同)

Pat*_*son 6

Tkinter处理来自多个线程的输入有问题,我使用mtTkinter代替,你不需要更改任何代码,一切都会正常工作.只需导入mtTkinter而不是Tkinter.

你可以在这里得到它:

http://tkinter.unpythonic.net/wiki/mtTkinter