enrm
enrm

Reputation: 705

wx Python subclass Dialog

I have a source file containing the following:

 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 )

Im trying to instantiate this in another source file like so:

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

However, I get the following error:

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 *'

Any idea why this is happening? I have tried passing wx.Window to the Dialog init but it doesnt make a difference. Can't someon explain why this is happening?

Upvotes: 0

Views: 244

Answers (1)

Selçuk Cihan
Selçuk Cihan

Reputation: 2034

dlg = Dialog1(parent_window)

is the way to go. Where parent_window will be the parent of the dialog. See classes in Python.

Quoting from the link:

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

Here is a working minimal code snippet:

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

Upvotes: 1

Related Questions