如何为wxpython接口创建良好的构造函数?

时间:2019-02-16 03:28:17

标签: wxpython

我使用wxpython创建了图形界面,但事实证明,当我关闭应用程序时,控制台出现错误,想知道我做错了什么。 我使用的版本

  • wxpython 4.0.4
  • python 3.5“ x64”

-代码-

import wx
import wx.xrc
import wx.aui
import gettext
_ = gettext.gettext
## Class MyFrame1
class MyFrame1 ( wx.Frame ):
    def __init__( self, parent, *args, **kwds ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 634,367 ), style = wx.DEFAULT_FRAME_STYLE )

        self.SetSizeHints( wx.DefaultSize, wx.DefaultSize )
        self.m_mgr = wx.aui.AuiManager()
        self.m_mgr.SetManagedWindow( self )

        self.m_mgr.SetFlags(wx.aui.AUI_MGR_ALLOW_ACTIVE_PANE|
        wx.aui.AUI_MGR_DEFAULT|wx.aui.AUI_MGR_LIVE_RESIZE)

        self.m_auinotebook1 = wx.aui.AuiNotebook( self, wx.ID_ANY,wx.DefaultPosition, wx.Size( 320,-1 ), wx.aui.AUI_NB_DEFAULT_STYLE )
        self.m_mgr.AddPane( self.m_auinotebook1, wx.aui.AuiPaneInfo() .Left() .PinButton( True ).Dock().Resizable().FloatingSize( wx.DefaultSize ) )

        self.m_panel2 = wx.Panel( self.m_auinotebook1, wx.ID_ANY,wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
        self.m_auinotebook1.AddPage( self.m_panel2, _(u"Inicio"), False,wx.NullBitmap )

        self.m_statusBar1 = self.CreateStatusBar( 1, wx.STB_SIZEGRIP, wx.ID_ANY )
        self.m_menubar1 = wx.MenuBar( 0 )
        self.m_menu1 = wx.Menu()
        self.m_menuItem1 = wx.MenuItem( self.m_menu1, wx.ID_ANY, _(u"Guardar"), wx.EmptyString, wx.ITEM_NORMAL )
        self.m_menu1.Append( self.m_menuItem1 )

        self.m_menu1.AppendSeparator()

        self.m_menuItem2 = wx.MenuItem( self.m_menu1, wx.ID_ANY, _(u"Salir"), wx.EmptyString, wx.ITEM_NORMAL )
        self.m_menu1.Append( self.m_menuItem2 )

        self.m_menubar1.Append( self.m_menu1, _(u"Inicio") )

        self.SetMenuBar( self.m_menubar1 )

        self.m_mgr.Update()
        self.Centre( wx.BOTH )

        # Connect Events
        self.Bind( wx.EVT_MENU, self.Save, id = self.m_menuItem1.GetId() )
        self.Bind( wx.EVT_MENU, self.Exit, id = self.m_menuItem2.GetId() )

    def __del__( self ):
        self.m_mgr.UnInit()

    # Virtual event handlers, overide them in your derived class
    def Save( self, event ):
        event.Skip()

    def Exit( self, event ):
        event.Skip()

if __name__ == "__main__":
    app = wx.App(redirect=False)
    frame = MyFrame1(None)
    frame.Show()
    app.MainLoop()

_________错误_______

该错误源自MainLoop(),但我不知道我在做什么

Traceback (most recent call last):
File "C:\Users\DennisM\Documents\Archivos de Outlook\proyecto.py", line 76, 
in <module>
app.MainLoop()
File "C:\Python35\lib\site-packages\wx\core.py", line 2166, in MainLoop
rv = wx.PyApp.MainLoop(self)
wx._core.wxAssertionError: C++ assertion "GetEventHandler() == this" failed 
at ..\..\src\common\wincmn.cpp(478) in wxWindowBase::~wxWindowBase(): any 
pushed event handlers must have been removed

1 个答案:

答案 0 :(得分:0)

您尚未在self.Destroy()中使用Exit,并且已经为__del__ AuiManager's编写了自己的UnInit方法。
Bind添加到窗口的EVT_CLOSE中,您应该有一个能够自动运行的程序。

import wx
import wx.xrc
import wx.aui
import gettext
_ = gettext.gettext
## Class MyFrame1
class MyFrame1 ( wx.Frame ):
    def __init__( self, parent, *args, **kwds ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 634,367 ), style = wx.DEFAULT_FRAME_STYLE )
        self.SetSizeHints( wx.DefaultSize, wx.DefaultSize )
        self.m_mgr = wx.aui.AuiManager()
        self.m_mgr.SetManagedWindow( self )

        self.m_mgr.SetFlags(wx.aui.AUI_MGR_ALLOW_ACTIVE_PANE|
        wx.aui.AUI_MGR_DEFAULT|wx.aui.AUI_MGR_LIVE_RESIZE)

        self.m_auinotebook1 = wx.aui.AuiNotebook( self, wx.ID_ANY,wx.DefaultPosition, wx.Size( 320,-1 ), wx.aui.AUI_NB_DEFAULT_STYLE )
        self.m_mgr.AddPane( self.m_auinotebook1, wx.aui.AuiPaneInfo() .Left() .PinButton( True ).Dock().Resizable().FloatingSize( wx.DefaultSize ) )

        self.m_panel2 = wx.Panel( self.m_auinotebook1, wx.ID_ANY,wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
        self.m_auinotebook1.AddPage( self.m_panel2, _(u"Inicio"), False,wx.NullBitmap )

        self.m_statusBar1 = self.CreateStatusBar( 1, wx.STB_SIZEGRIP, wx.ID_ANY )
        self.m_menubar1 = wx.MenuBar( 0 )
        self.m_menu1 = wx.Menu()
        self.m_menuItem1 = wx.MenuItem( self.m_menu1, wx.ID_ANY, _(u"Guardar"), wx.EmptyString, wx.ITEM_NORMAL )
        self.m_menu1.Append( self.m_menuItem1 )

        self.m_menu1.AppendSeparator()

        self.m_menuItem2 = wx.MenuItem( self.m_menu1, wx.ID_ANY, _(u"Salir"), wx.EmptyString, wx.ITEM_NORMAL )
        self.m_menu1.Append( self.m_menuItem2 )

        self.m_menubar1.Append( self.m_menu1, _(u"Inicio") )

        self.SetMenuBar( self.m_menubar1 )

        self.m_mgr.Update()
        self.Centre( wx.BOTH )

        # Connect Events
        self.Bind( wx.EVT_MENU, self.Save, id = self.m_menuItem1.GetId() )
        self.Bind( wx.EVT_MENU, self.Exit, id = self.m_menuItem2.GetId() )
        self.Bind( wx.EVT_CLOSE, self.Exit)

    #def __del__( self ):
    #    self.m_mgr.UnInit()

    # Virtual event handlers, overide them in your derived class
    def Save( self, event ):
        event.Skip()

    def Exit( self, event ):
        self.m_mgr.UnInit()
        self.Destroy()

if __name__ == "__main__":
    app = wx.App(redirect=False)
    frame = MyFrame1(None)
    frame.Show()
    app.MainLoop()