【发布时间】:2014-12-29 11:24:28
【问题描述】:
听说Python中的线程不好处理,和tkinter比较纠结。
我有以下问题。我有两个类,一个用于 GUI,另一个用于无限进程。首先,我启动 GUI 类,然后启动无限进程类。我希望当你关闭 GUI 时,它也完成了无限过程并且程序结束。
代码的简化版本如下:
import time, threading
from tkinter import *
from tkinter import messagebox
finish = False
class tkinterGUI(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
global finish
#Main Window
self.mainWindow = Tk()
self.mainWindow.geometry("200x200")
self.mainWindow.title("My GUI Title")
#Label
lbCommand = Label(self.mainWindow, text="Hello world", font=("Courier New", 16)).place(x=20, y=20)
#Start
self.mainWindow.mainloop()
#When the GUI is closed we set finish to "True"
finish = True
class InfiniteProcess(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
global finish
while not finish:
print("Infinite Loop")
time.sleep(3)
GUI = tkinterGUI()
GUI.start()
Process = InfiniteProcess()
Process.start()
当我点击关闭按钮(在右上角)时,控制台中出现以下错误:
Tcl_AsyncDelete: async handler deleted by the wrong thread
我不知道它为什么会发生或它意味着什么。
【问题讨论】:
-
您的简化版本对我来说没问题...一定是您忘记添加导致问题的原因
-
@mguijarr 我在 google 中读到这个错误在 Window 中更常见,你的 SO 是什么?我的是 Windows 7 x64。也许窗户是问题:/
标签: python python-3.x multithreading tkinter tcl