wx Python 子类对话框

wx Python subclass Dialog

我有一个包含以下内容的源文件:

 class Dialog1 ( wx.Dialog ):

    def __init__( self, parent ):

        wx.Dialog.__init__ ( self, parent, id = wx.ID_ANY, title = u"Hello", pos = wx.DefaultPosition, size = wx.Size( 342,253 ), style = wx.DEFAULT_DIALOG_STYLE )

我正在尝试在另一个源文件中实例化它,如下所示:

dlg = Dialog1(wx.Dialog).__init__(self, None)

但是,我收到以下错误:

Traceback (most recent call last):
File "scripts\motors\motors.py", line 281, in onListOption
dlg = Dialog1(wx.Dialog).__init__(self, None)
File "...",
line 21, in __init__
    wx.Dialog.__init__ ( self, parent, id = wx.ID_ANY, title = u"Hello", pos = wx.DefaultPosition, size = wx.Size( 342,253 ), style = wx.DEFAULT_DIALOG_STYLE )
  File "c:\Python27\lib\site-packages\wx-3.0-msw\wx\_windows.py", line 734, in __init__
_windows_.Dialog_swiginit(self,_windows_.new_Dialog(*args, **kwargs))
TypeError: in method 'new_Dialog', expected argument 1 of type 'wxWindow *'

知道为什么会这样吗?我试过将 wx.Window 传递给 Dialog init,但它没有什么区别。有人不能解释为什么会这样吗?

dlg = Dialog1(parent_window)

是必经之路。其中 parent_window 将是对话框的父级。参见 classes in Python

引自link:

When a class defines an init() method, class instantiation automatically invokes init() for the newly-created class instance.

这是一个有效的最小代码片段:

import wx

class Dialog1 ( wx.Dialog ):
    def __init__( self, parent ):
        wx.Dialog.__init__ ( self, parent, id = wx.ID_ANY, title = u"Hello",
            pos = wx.DefaultPosition, size = wx.Size( 342,253 ),
            style = wx.DEFAULT_DIALOG_STYLE )

app = wx.App()
dlg = Dialog1(None)
dlg.Show()
app.MainLoop()