How to creat a new file-select window as I click a button?

I want to popup a sole file-select window as I click a button, I
searched it in the email list, nothing was found. Here is my code,
thank in advance :slight_smile:

import wx

class TopFrame(wx.Frame):

    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, "My first GUI APP", size=
(200, 300))
        self.panel = wx.Panel(self, -1)
        self.panel.SetBackgroundColour("White")

        self.buttona = wx.Button(self.panel, -1, label="Open File",
style=wx.BU_LEFT)
        self.Bind(wx.EVT_BUTTON, self.OpenFile, self.buttona)

    def OpenFile():
        file = wx.FileDialog(None, message="Choose a file",
defaultDir="", defaultFile="", wildcard="*.*", style=wx.OPEN,
pos=wx.DefaultPosition)
        if dialog.ShowModal() == wx.ID_OK:
            print dialog.GetPath()

        dialog.Destroy()

    def OnButtonClick(self, event):
        self.panel.SetBackgroundColour('Green')
        self.panel.Refresh()

def main():
    app = wx.PySimpleApp()
    frame = TopFrame(parent=None, id=-1)
    frame.Show()
    app.MainLoop()

if __name__ == '__main__':
    main()

Hi,

···

On Sun, May 31, 2009 at 4:35 AM, guoguo 999guo@gmail.com wrote:

I want to popup a sole file-select window as I click a button, I

searched it in the email list, nothing was found. Here is my code,

thank in advance :slight_smile:

import wx

class TopFrame(wx.Frame):

def __init__(self, parent, id):

    wx.Frame.__init__(self, parent, id, "My first GUI APP", size=

(200, 300))

    self.panel = wx.Panel(self, -1)

    self.panel.SetBackgroundColour("White")



    self.buttona = wx.Button(self.panel, -1, label="Open File",

style=wx.BU_LEFT)

    self.Bind(wx.EVT_BUTTON, self.OpenFile, self.buttona)



def OpenFile():

    file = wx.FileDialog(None, message="Choose a file",

defaultDir=“”, defaultFile=“”, wildcard=“.”, style=wx.OPEN,

pos=wx.DefaultPosition)

    if dialog.ShowModal() == wx.ID_OK:

        print dialog.GetPath()



    dialog.Destroy()





def OnButtonClick(self, event):

    self.panel.SetBackgroundColour('Green')

    self.panel.Refresh()

def main():

app = wx.PySimpleApp()

frame = TopFrame(parent=None, id=-1)

frame.Show()

app.MainLoop()

if name == ‘main’:

main()

So what is the problem exactly? The code looks like it should work to me.

  • Mike

You are assigning "file = wx.FileDialog(...)" but referring to "if
dialog.ShowModal() ==..."

Use "dialog" instead of "file" as your variable. file is a reserved
word in Python, I think.

Josh

···

On 5/31/09, Mike Driscoll <mike@pythonlibrary.org> wrote:

Hi,

On Sun, May 31, 2009 at 4:35 AM, guoguo <999guo@gmail.com> wrote:
>
> I want to popup a sole file-select window as I click a button, I
> searched it in the email list, nothing was found. Here is my code,
> thank in advance :slight_smile:
>
> import wx
>
> class TopFrame(wx.Frame):
>
> def __init__(self, parent, id):
> wx.Frame.__init__(self, parent, id, "My first GUI APP", size=
> (200, 300))
> self.panel = wx.Panel(self, -1)
> self.panel.SetBackgroundColour("White")
>
> self.buttona = wx.Button(self.panel, -1, label="Open File",
> style=wx.BU_LEFT)
> self.Bind(wx.EVT_BUTTON, self.OpenFile, self.buttona)
>
> def OpenFile():
> file = wx.FileDialog(None, message="Choose a file",
> defaultDir="", defaultFile="", wildcard="*.*", style=wx.OPEN,
> pos=wx.DefaultPosition)
> if dialog.ShowModal() == wx.ID_OK:
> print dialog.GetPath()
>
> dialog.Destroy()
>
>
> def OnButtonClick(self, event):
> self.panel.SetBackgroundColour('Green')
> self.panel.Refresh()
>
>
>
> def main():
> app = wx.PySimpleApp()
> frame = TopFrame(parent=None, id=-1)
> frame.Show()
> app.MainLoop()
>
> if __name__ == '__main__':
> main()
>
>

So what is the problem exactly? The code looks like it should work to me.

- Mike

>

--
Josh English
Joshua.R.English@gmail.com

sorry, “file” was a slip of the pen,I changed it to dialoga, but can't
popup window either when I clicked button,
error message:TypeError: OpenFile() takes no arguments (2 given)

import wx

class TopFrame(wx.Frame):

    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, "My first GUI APP", size=
(200, 100))
        self.panel = wx.Panel(self, -1)
        self.panel.SetBackgroundColour("White")

        self.buttona = wx.Button(self.panel, -1, label="Open File",
style=wx.BU_LEFT)
        self.Bind(wx.EVT_BUTTON, self.OpenFile, self.buttona)

    def OpenFile():
        dialoga = wx.FileDialog(None, message="Choose a file",
defaultDir="", defaultFile="", wildcard="*.*", style=wx.OPEN,
pos=wx.DefaultPosition)
        if dialoga.ShowModal() == wx.ID_OK:
            print dialoga.GetPath()
        dialoga.Destroy()

    def OnButtonClick(self, event):
        self.panel.SetBackgroundColour('Green')
        self.panel.Refresh()

def main():
    app = wx.PySimpleApp()
    frame = TopFrame(parent=None, id=-1)
    frame.Show()
    app.MainLoop()

if __name__ == '__main__':
    main()

···

On Jun 1, 8:03 am, Josh English <joshua.r.engl...@gmail.com> wrote:

You are assigning "file = wx.FileDialog(...)" but referring to "if
dialog.ShowModal() ==..."

Use "dialog" instead of "file" as your variable. file is a reserved
word in Python, I think.

Josh

On 5/31/09, Mike Driscoll <m...@pythonlibrary.org> wrote:

> Hi,

> On Sun, May 31, 2009 at 4:35 AM, guoguo <999...@gmail.com> wrote:

> > I want to popup a sole file-select window as I click a button, I
> > searched it in the email list, nothing was found. Here is my code,
> > thank in advance :slight_smile:

> > import wx

> > class TopFrame(wx.Frame):

> > def __init__(self, parent, id):
> > wx.Frame.__init__(self, parent, id, "My first GUI APP", size=
> > (200, 300))
> > self.panel = wx.Panel(self, -1)
> > self.panel.SetBackgroundColour("White")

> > self.buttona = wx.Button(self.panel, -1, label="Open File",
> > style=wx.BU_LEFT)
> > self.Bind(wx.EVT_BUTTON, self.OpenFile, self.buttona)

> > def OpenFile():
> > file = wx.FileDialog(None, message="Choose a file",
> > defaultDir="", defaultFile="", wildcard="*.*", style=wx.OPEN,
> > pos=wx.DefaultPosition)
> > if dialog.ShowModal() == wx.ID_OK:
> > print dialog.GetPath()

> > dialog.Destroy()

> > def OnButtonClick(self, event):
> > self.panel.SetBackgroundColour('Green')
> > self.panel.Refresh()

> > def main():
> > app = wx.PySimpleApp()
> > frame = TopFrame(parent=None, id=-1)
> > frame.Show()
> > app.MainLoop()

> > if __name__ == '__main__':
> > main()

> So what is the problem exactly? The code looks like it should work to me.

> - Mike

--
Josh English
Joshua.R.Engl...@gmail.comhttp://joshenglish.livejournal.com

Thanks Mike and Josh, I have resolved this problem,
I just changed "def OpenFile():" to "def OpenFile(self, evt): "
But I don't know why to add "evt" in it, could you please tell me why?

nice day! :slight_smile:

oops, I missed that, too.

You need 'self' because OnFile is a method of a class object. This a
a Python rule.

wxPython events are passed to the handler. The event itself is loaded
with all sorts of information: where on the screen the mouse is (if
applicable), the string of the text, or even the client object
associated with the event. In your case, the event object will give
you access to the button that was clicked, Look up wxCommandEvent in
the docs and you'll see the sort of things the event object can give
you. Not all of it is appropriate for a button, though. For example, I
don't think button can hold client data, but it would be cool if they
could.

If you don't know what those are, don't worry, you'll get there. This
is a big thing to learn. There are lots of details to manage, and you
learn to manage them by coding, fixing errors, and coding more.

···

On 5/31/09, guoguo <999guo@gmail.com> wrote:

Thanks Mike and Josh, I have resolved this problem,
I just changed "def OpenFile():" to "def OpenFile(self, evt): "
But I don't know why to add "evt" in it, could you please tell me why?

nice day! :slight_smile:

>

--
Josh English
Joshua.R.English@gmail.com

Hi,

···

On Sun, May 31, 2009 at 11:33 PM, Josh English joshua.r.english@gmail.com wrote:

oops, I missed that, too.

You need ‘self’ because OnFile is a method of a class object. This a

a Python rule.

wxPython events are passed to the handler. The event itself is loaded

with all sorts of information: where on the screen the mouse is (if

applicable), the string of the text, or even the client object

associated with the event. In your case, the event object will give

you access to the button that was clicked, Look up wxCommandEvent in

the docs and you’ll see the sort of things the event object can give

you. Not all of it is appropriate for a button, though. For example, I

don’t think button can hold client data, but it would be cool if they

could.

If you don’t know what those are, don’t worry, you’ll get there. This

is a big thing to learn. There are lots of details to manage, and you

learn to manage them by coding, fixing errors, and coding more.

On 5/31/09, guoguo 999guo@gmail.com wrote:

Thanks Mike and Josh, I have resolved this problem,

I just changed “def OpenFile():” to "def OpenFile(self, evt): "

But I don’t know why to add “evt” in it, could you please tell me why?

nice day! :slight_smile:

Josh English

Man, I totally missed two problems with that code. I guess I was out of it yesterday. Anyway, when you bind an event, you end up passing the event to the event handler. The “self” is a reference to the instance of the object, which is usually the wx.Frame or some other top window. If you do this, you can find out what it is:

print type(self)

(This only works if you can see stdout, of course).

The idea here is that you could potentially have multiple instances that behave differently, but you need a way to refer to them. As I recall, Visual Basic called it “this” instead of “self”. You can call it whatever you like, but the convention you should use is definitely “self” as everyone else does that.

The only other thing I think I should mention is that you should look for Chris Barker’s threads on using lambda for event processing. It works slightly differently, but it’s a good thing to know about since Tkinter uses it default.

  • Mike

Josh English wrote:

You are assigning "file = wx.FileDialog(...)" but referring to "if
dialog.ShowModal() ==..."

Use "dialog" instead of "file" as your variable. file is a reserved
word in Python, I think.

Not reserved, but it is the name of a built-in function.

···

--
Robin Dunn
Software Craftsman

got it! thanks everybody,
and happy Children's Day!!! :slight_smile:

Ah yes. It's a technical difference, but as far as I'm concerned it
means "Don't use this as a variable name."

Josh

···

On Mon, Jun 1, 2009 at 11:09 AM, Robin Dunn <robin@alldunn.com> wrote:

Josh English wrote:

Use "dialog" instead of "file" as your variable. file is a reserved
word in Python, I think.

Not reserved, but it is the name of a built-in function.

--
Robin Dunn
Software Craftsman
http://wxPython.org

>

--
Josh English
Joshua.R.English@gmail.com

Josh English wrote:

Ah yes. It's a technical difference, but as far as I'm concerned it
means "Don't use this as a variable name."

Agreed.

···

--
Robin Dunn
Software Craftsman