【发布时间】:2016-10-15 21:16:56
【问题描述】:
我目前正在创建一个小程序,它将 Tkinter 输入框插入到 Google 日历中(当然,在所有不同类型的检查之后)。那部分不是问题。 由于我同时在运行一个终端,我不想在 Tkinter 窗口打开期间“保持”。
当我使用
关闭窗口时def quit(self):
self.thread.stop()
self.destroy()
窗口的所有部分都消失了,但窗口仍然在屏幕上。
class window(Frame):
def __init__(self, parent, thread):
Frame.__init__(self, parent)
self.parent = parent
self.w = 600
self.h = 400
self.initUI()
self.center()
self.thread = thread
我使用这个函数来创建类:
def main(thread):
root = Tk()
root.resizable(width=False, height=False)
app = window(root, thread)
root.mainloop()
myThread 类。
class myThread(threading.Thread):
def __init__(self,threadType):
threading.Thread.__init__(self)
self.threadType = threadType
self.event = threading.Event()
def stop(self):
self.event.set()
def run(self):
if self.threadType == "new_app":
newappoint.main(self)
elif self.threadType == "main":
main()
谁能告诉我我是否错过了可以使窗户正常关闭的东西。
调用self.quit()后线程关闭
【问题讨论】:
-
tkinter 和线程不能很好地混合,例如参见 stackoverflow.com/questions/10556479/…
-
@shivsn 成功了,谢谢 :)
-
@J.J.Hakala 所以基本上最好只线程化我原来的主函数然后保持
if __name__ == __main__:循环寻找一个变量来改变和启动Tkinter?
标签: python multithreading tkinter