本文介绍了创建第二个顶级小部件时,线程化的Tkinter脚本崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Tkinter作为GUI的Python脚本.我的小脚本应该每X秒创建一个顶级小部件.当我运行代码时,第一个顶级"窗口小部件已成功创建,但是当它尝试创建第二个窗口小部件"时,程序崩溃了.

I have a Python script which uses Tkinter for the GUI. My little script should create a Toplevel widget every X seconds. When I run my code, the first Toplevel widget is created successfully, but when it tries to create a second one the program crashes.

我正在做的是使用after方法每隔5秒钟与root的mainloop一起调用函数startCounting.每次调用此函数时,我都会将Toplevel小部件对象追加到列表中,并启动一个新线程,希望该线程将运行新的mainloop.

What I am doing is using the after method to call the function startCounting every 5 seconds alongside root's mainloop. Every time this function is called, I append a Toplevel widget object into a list and start a new thread which hopefully will be running the new mainloop.

如果有人能解决这个问题,我将不胜感激.顺便说一下,这只是我目前正在使用的一个小脚本来解决我的问题,这使我无法继续进行实际的学校项目.

I would be very grateful if someone could figure this problem out. By the way, this is just a little script that I am currently using to solve my problem, which is preventing me from going on with my real school project.

代码:

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设计为仅从主线程运行.参见文档:

Tkinter is designed to run from the main thread, only. See the docs:

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

...and a substantial example follows, showing secondary threads writing requests to a queue, and the main loop being exclusively responsible for all direct interactions with Tk.

许多对象和子系统不喜欢接收来自多个线程的请求,在使用GUI工具箱的情况下,不需要专门使用 main 线程.

Many objects and subsystems don't like receiving requests from multiple various threads, and in the case of GUI toolkit it's not rare to need specfically to use the main thread only.

此问题的正确Python体系结构始终是为服务挑剔的对象或子系统分配一个线程(如果需要,则指定一个主线程).所有其他需要与所述子系统或对象进行交互的线程都必须通过将请求排队到专用线程来获得它(如果由于某些请求而需要结果,则可能在返回队列"中等待结果).这也是用于通用线程的非常完善的Python体系结构(我在"Nutshell中的Python"中对此进行了详尽的阐述,但这是另一个主题;-).

The right Python architecture for this issue is always to devote a thread (the main one, if one must) to serving the finicky object or subsystem; every other thread requiring interaction with said subsystem or object must them obtain it by queueing requests to the dedicated thread (and possibly waiting on a "return queue" for results, if results are required as a consequence of some request). This is also a very sound Python architecture for general-purpose threading (and I expound on it at length in "Python in a Nutshell", but that's another subject;-).

这篇关于创建第二个顶级小部件时,线程化的Tkinter脚本崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 02:58