Is there an easy way round this, or should I code it as a method of
the main class ?
Cheers
Dave
Well, normally you don’t subclass something and then create an instance of the same object in its init. Instead, I’d just create the dialog instance in an event handler in your frame or panel. You can use almost all the code you have in the init too. There’s a simple example showing one way to do it here: http://www.blog.pythonlibrary.org/2010/06/26/the-dialogs-of-wxpython-part-1-of-2/
Is there an easy way round this, or should I code it as a method of
the main class ?
Cheers
Dave
Well, normally you don't subclass something and then create an instance of
the same object in its __init__. Instead, I'd just create the dialog
instance in an event handler in your frame or panel. You can use almost all
the code you have in the __init__ too. There's a simple example showing one
way to do it here: http://www.blog.pythonlibrary.org/2010/06/26/the-dialogs-of-wxpython-part-1-of-2/
You could also code this with a few less lines and fully ensure that dlg.Destroy() is called by doing:
with wx.FileDialog(self, _(u'Choose a file'),
defDir, "", "*.*",
wx.OPEN | wx.FD_FILE_MUST_EXIST) as dlg:
if dlg.ShowModal() == wx.ID_OK:
filename = dlg.GetPath()
'with' is available as of Py 2.5 and wxPython added the context manager support in 2.8.11.0.
I just very recently caught on to this when Walter mentioned it in another thread.