我在wxPython应用程序中使用Twisted库来支持网络,但当我尝试在wx中使用Twisted reactor时。应用程序的OnExit应用程序从不关闭(见下文)。
如果我尝试执行反应堆。停止()以防止wx被破坏。对话框工作正常。问题是,首先显示登录对话框,然后显示主对话框,因此必须复制停堆代码并放置反应堆。停止()所有的地方看起来都是糟糕的设计。
我正在尝试做的是可能的,如果是的话,我缺少什么来实现这一点,因为我不明白为什么反应堆在被告知时没有停止。
import wx from twisted.internet import wxreactor wxreactor.install() from twisted.internet import reactor class LogonDialog(wx.Dialog): def __init__(self, parent): super(LogonDialog, self).__init__(None, title = 'LogonDialogTitle' , size = (300, 200) , style = wx.CLOSE_BOX | wx.CAPTION | wx.SYSTEM_MENU | wx.RESIZE_BORDER) self.Center() self.Bind(wx.EVT_CLOSE, self.OnClose) def OnClose(self, event): dlgResult = wx.MessageBox('Are you sure you want to quit?', 'Confirm Quit', wx.YES_NO | wx.ICON_QUESTION); if dlgResult == wx.YES: self.Destroy() class Client(wx.App): def OnInit(self): lg = LogonDialog(self) lg.Show(True) return True def OnExit(self): if reactor.running: reactor.stop() client=Client(0) reactor.registerWxApp(client) reactor.run()
环境
如果更换 self.Destroy() 使用 reactor.stop() 调用,然后应用程序干净地退出。
self.Destroy()
reactor.stop()
我不清楚这是wxreactor中的一个bug,还是记录在案的缺陷/约束。从文档中,请注意:
Stop the event loop using reactor.stop(), not yourApp.ExitMainLoop().
你还没打电话 yourApp.ExitMainLoop() 但我想知道 自己销毁() 基本上是等效的。不管怎样,文档中非常清楚地指出您应该使用 核反应堆停止() 当您的应用程序完成时。
yourApp.ExitMainLoop()
自己销毁()
核反应堆停止()