Hello everyone!
I’m new to wxpython. I’m trying to make a simple GUI to open a file. I tried something like this -
import wx
import wx
class abc(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,'Frame aka window', size=(300,200))
panel=wx.Panel(self)
button=wx.Button(panel,label="open",pos=(130,10),size=(60,60))
self.Bind(wx.EVT_BUTTON, self.filebutton, button)
self.Bind(wx.EVT_CLOSE, self.closewindow)
def filebutton(self,event):
filename = wx.FileSelector(message = ('choose file to open'), default_path = wx.GetHomeDir(), default_filename = 'evaluation.doc',)
def closewindow(self,event):
self.Destroy()
if name==‘main’ :
app=wx.PySimpleApp()
frame=abc(parent=None,id=-1)
frame.Show()
app.MainLoop()
Now, what happens is when I run this on my terminal(I’m using UBUNTU 12.04 btw)…
I get this window -

which is fine.
When i click on “open”, the file selector window opens and I select the desired file and then choose the open option but nothing happens. The file selector window closes and the above window remains.
What am I doing wrong? Why isn’t the file opening?
Please help and answer in full detail as I’m new to this.
Thanks! 
You don't do anything to retrieve the file path or to check if the user pressed "Open" or "Cancel".
I don't know about wx.FileSelector,
but here's a file-open snippet from my code.
You can find good examples for this sort of thing in the demo.
dlg = wx.FileDialog(
self, message=u'Backup-Datei auswählen',
defaultDir='',
wildcard=wildcard,
style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST
)
if dlg.ShowModal() == wx.ID_OK:
os.startfile(dlg.GetPath()) # Startfile is Windows-only I think
dlg.Destroy()
Regards,
Michael
···
On Mon, 30 Jun 2014 17:11:54 +0200, Ankita Juneja <ankitaj.pec@gmail.com> wrote:
*Hello everyone! I'm new to wxpython. I'm trying to make a simple GUI to
open a file. I tried something like this - *
import wx
import wx
class abc(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,'Frame aka window', size=(300,200))
panel=wx.Panel(self)
button=wx.Button(panel,label="open",pos=(130,10),size=(60,60))
self.Bind(wx.EVT_BUTTON, self.filebutton, button)
self.Bind(wx.EVT_CLOSE, self.closewindow)
def filebutton(self,event):
filename = wx.FileSelector(message = ('choose file to open'),
default_path = wx.GetHomeDir(), default_filename = 'evaluation.doc',)
def closewindow(self,event):
self.Destroy()
if __name__=='__main__' :
app=wx.PySimpleApp()
frame=abc(parent=None,id=-1)
frame.Show()
app.MainLoop()
*Now, what happens is*
*when I run this on my terminal(I'm using UBUNTU 12.04 btw).. I get this
window -*
<https://lh4.googleusercontent.com/-xRPJybL3v2I/U7Fzaojo5bI/AAAAAAAAAF4/OR3KITCqnhw/s1600/Screenshot+from+2014-06-30+19%3A52%3A53.png>
*which is fine.When i click on "open", the file selector window opens and I
select the desired file and then choose the open option but nothing
happens. The file selector window closes and the above window remains.What
am I doing wrong? Why isn't the file opening?*
Michael gave you already some pointers.
I would add the following.
- don’t use fixed sizes and positions but use sizers, a bit
difficult to learn but you will not regret it
- wx.PySimpleApp is deprecated, you can just use wx.App or the WIT
I attach a reworked sample, here some comments to it.
- I like the sized_controls as they automate a lot of the sizer
related code -
wxpython.org/Phoenix/docs/html/lib.sized_controls.html
- to debug what goes wrong the WIT is a very useful tool -
- I used ‘with’ context manager for the dialog, it ensures that what
ever happens the dialog will be destroyed
Note that the Phoenix documentation link above is for an up coming
version of wxPython, so not all is the same as in the current
version of wxPython, but I prefer it to the C++ docs.
Have fun with wxPython!
Werner
fdSample.py (1.09 KB)
···
Hi,
On 6/30/2014 17:11, Ankita Juneja wrote:
everyone!
I’m new to wxpython. I’m trying to make a simple GUI to open a
file. I tried something like this -*
http://wiki.wxpython.org/Widget%20Inspection%20Tool
This was just my first post here and I must say, I’m really overwhelmed by the response! Thankyou Michael and Werner! 
With your help, I’m now finally able to achieve what I wanted. I would like to share it with you…
def filebutton(self, event):
with wx.FileDialog(self,
message='Choose file to open',
defaultDir=wx.GetHomeDir(),
defaultFile='evaluation.doc',
style=wx.FD_OPEN) as dlgF:
if dlgF.ShowModal() == wx.ID_OK:
fileName = dlgF.GetPath()
subprocess.call(["xdg-open", fileName])
Since neither “desktop.open()” nor “os.startfile()” work in linux ubuntu, I guess(atleast these didn’t work for me), I tried this and it worked!
Also, thanks for telling me about the sizers and WIT. I guess I’ll have to do some reading on that now.
Regards,
Ankita.
Great that you got it working!
The desktop module should hide these platform differences, but it is
not included out of the box, i.e. you have to install it - I should
have mentioned that.
Have fun
Werner
···
Hi Ankita,
On 6/30/2014 22:04, Ankita Juneja wrote:
This was just my first post here and I must say,
I’m really overwhelmed by the response! Thankyou Michael and
Werner! 
With your help, I'm now finally able to achieve what I wanted.
I would like to share it with you…
def
filebutton(self, event):
with
wx.FileDialog(self,
message=‘Choose file to open’,
defaultDir=wx.GetHomeDir(),
defaultFile=‘evaluation.doc’,
style=wx.FD_OPEN) as dlgF:
if dlgF.ShowModal() == wx.ID_OK:
fileName = dlgF.GetPath()
subprocess.call([“xdg-open”, fileName])
Since neither "desktop.open()" nor "os.startfile()" work in
linux ubuntu, I guess(atleast these didn’t work for me), I
tried this and it worked!
https://pypi.python.org/pypi/desktop
Also, thanks for telling me about the sizers and WIT. I guess
I’ll have to do some reading on that now.