Newbe Generic FileDialog class, return values issue,

Hi, I am trying to write a generic open dialog, the following works
but complains that __init__ has to return None ...

class _OpenDialog(wx.FileDialog):

    def __init__(self, parent, title, file_name, wild_card, file_):

        dlg = wx.FileDialog(parent, title, "", file_name, wild_card,
wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
        if dlg.ShowModal() == wx.ID_OK:
            dlg.Destroy()
            return (dlg.GetDirectory(), dlg.GetFilename())

        else:
            dlg.Destroy()
            return(False, '')

Is there an easy way round this, or should I code it as a method of
the main class ?

Cheers

Dave

···

--

Please avoid sending me Word or PowerPoint attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html

Hi,

···

On Tuesday, February 28, 2012 3:19:36 PM UTC-6, kbmaniac wrote:

Hi, I am trying to write a generic open dialog, the following works
but complains that init has to return None …
class _OpenDialog(wx.FileDialog):

def init(self, parent, title, file_name, wild_card, file_):

dlg = wx.FileDialog(parent, title, “”, file_name, wild_card,
wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
if dlg.ShowModal() == wx.ID_OK:
dlg.Destroy()
return (dlg.GetDirectory(), dlg.GetFilename())

else:
dlg.Destroy()
return(False, ‘’)

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/

  • Mike

Thanks, newbe still learning and making mistakes :slight_smile:

Dave

···

On 28 February 2012 21:26, Mike Driscoll <kyosohma@gmail.com> wrote:

Hi,

On Tuesday, February 28, 2012 3:19:36 PM UTC-6, kbmaniac wrote:

Hi, I am trying to write a generic open dialog, the following works
but complains that __init__ has to return None ...

class _OpenDialog(wx.FileDialog):

def \_\_init\_\_\(self, parent, title, file\_name, wild\_card, file\_\):

    dlg = wx\.FileDialog\(parent, title, &quot;&quot;, file\_name, wild\_card,

wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
if dlg.ShowModal() == wx.ID_OK:
dlg.Destroy()
return (dlg.GetDirectory(), dlg.GetFilename())

    else:
        dlg\.Destroy\(\)
        return\(False, &#39;&#39;\)

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/

- Mike

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

--

Please avoid sending me Word or PowerPoint attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html

Hi Dave,

...

         dlg = wx.FileDialog(parent, title, "", file_name, wild_card,
wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
         if dlg.ShowModal() == wx.ID_OK:
             dlg.Destroy()
             return (dlg.GetDirectory(), dlg.GetFilename())

         else:
             dlg.Destroy()
             return(False, '')

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.

Werner

···

On 28/02/2012 22:27, dave selby wrote: