代码之家  ›  专栏  ›  技术社区  ›  Swatcat

扭曲的WX反应堆未在wxPython中闭合

  •  0
  • Swatcat  · 技术社区  · 6 年前

    我在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()
    

    环境

    • Python:V2。7.14
    • wxPython:V3。0.2
    • 扭曲:V17。9
    1 回复  |  直到 6 年前
        1
  •  1
  •   Jean-Paul Calderone    6 年前

    如果更换 self.Destroy() 使用 reactor.stop() 调用,然后应用程序干净地退出。

    我不清楚这是wxreactor中的一个bug,还是记录在案的缺陷/约束。从文档中,请注意:

    Stop the event loop using reactor.stop(), not yourApp.ExitMainLoop().
    

    你还没打电话 yourApp.ExitMainLoop() 但我想知道 自己销毁() 基本上是等效的。不管怎样,文档中非常清楚地指出您应该使用 核反应堆停止() 当您的应用程序完成时。