[Tutor] [wxPython-users] How to save file name of file opened from wx.FileDialog ?

Varsha Purohit varsha.purohit at gmail.com
Sun Nov 18 03:31:26 CET 2007


HI Alan,
    Thanks for suggestion its working now....

import wx
import os

class ScrolledWindow(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(350, 300))
        panel = wx.Panel(self, -1)
        self.txt1 = wx.TextCtrl(panel, -1, pos=(30, 100), size=(150, 20))
        self.button11 = wx.Button(panel, -1, "Open", pos=(200,100))
        self.button11.Bind(wx.EVT_BUTTON, self.OnOpen)

        self.Centre()
        self.Show()

    def OnOpen(self,event):
        self.dirname = ''
        dlg = wx.FileDialog(self, "Choose a file", self.dirname,"",
"*.*", wx.OPEN)
        if dlg.ShowModal()==wx.ID_OK:
            self.filename=dlg.GetFilename()
           self.dirname=dlg.GetDirectory()

            self.txt1.write(self.filename)
        dlg.Destroy()


app = wx.App()
ScrolledWindow(None, -1, 'Aliens')
app.MainLoop()

On Nov 17, 2007 12:44 PM, Varsha Purohit <varsha.purohit at gmail.com> wrote:
> Hello Everyone,
>         In my application i need to select a file using open dialog
> box. And then i dont need to open the file. I just need to display the
> name of the selected file in a text control. And then open the file in
> later part of the program.  But i am not able to get the file name and
> display it in the text control. Here is the sample code.
>
> import wx
> import os
>
> Fname = '' #Global variable to hold the file name.
> class ScrolledWindow(wx.Frame):
>     def __init__(self, parent, id, title):
>         wx.Frame.__init__(self, parent, id, title, size=(350, 300))
>         panel = wx.Panel(self, -1)
>         txt1 = wx.TextCtrl(panel, -1, pos=(30, 100), size=(150, 20))
>         button11 = wx.Button(panel, -1, "Open", pos=(200,100))
>         self.Bind(wx.EVT_BUTTON, self.OnOpen, button11)
>         txt1.write(Fname)
>
>         self.Centre()
>         self.Show()
>
>     def OnOpen(self,event):
>         self.dirname = ''
>         dlg = wx.FileDialog(self, "Choose a file", self.dirname,"",
> "*.*", wx.OPEN)
>         if dlg.ShowModal()==wx.ID_OK:
>             self.filename=dlg.GetFilename()
>             Fname = self.filename
>             self.dirname=dlg.GetDirectory()
>         dlg.Destroy()
>
> app = wx.App()
> ScrolledWindow(None, -1, 'Aliens')
> app.MainLoop()
>
>
> Any help is appreciated....
>
> thanks,
> --
> Varsha Purohit,
> Graduate Student
>


More information about the Tutor mailing list