我试图使用tkinter并在python 3中扭曲。我已经按照这个答案,Threading With Twisted and Tkinter,它允许我让它运行。
但问题是当代码崩溃或用户关闭时,反应堆不会停止。如何设置它以使reactor成为tkinter程序的守护程序。到目前为止,当我关闭tkinter gui时,我试图让它关闭反应堆,这不是理想的行为(因为我想在任何失败的情况下都关闭),但如果我能得到它,那将是第一步上班。
我的代码如下
from twisted.internet import reactor, protocol
import tkinter as tk
import tksupport
import GUI
def on_closing():
reactor.stop()
tksupport.uninstall()
root.destroy()
root = GUI.BruGUI()
tksupport.install(root)
root.protocol("WM_DELETE_WINDOW", on_closing)
reactor.run()
tksupport代码来自上一个链接。
一种简单的方法是将它放在try / except块中,但这通常只是一种解决方法:
try:
from twisted.internet import reactor, protocol
import tkinter as tk
import tksupport
import GUI
def on_closing():
reactor.stop()
tksupport.uninstall()
root.destroy()
root = GUI.BruGUI()
tksupport.install(root)
root.protocol("WM_DELETE_WINDOW", on_closing)
reactor.run()
except:
reactor.stop()
tksupport.uninstall()
root.destroy()