Reputation:
I'm starting to learn wxPython to create GUI apps, and I am facing some problem: When using wx.DirDialog to pick some folder, no matter what the folder I chose, dlg.GetPath() always return the same folder, which is in my case: /home/loai
my binding function is as follows, it is straight forward :
def onButton(self,e):
dlg = wx.DirDialog(self, "Choose a directory:")
if dlg.ShowModal() == wx.ID_OK:
print "You chose %s" % dlg.GetPath()
dlg.Destroy()
this always prints :You chose /home/loai
thanks
Upvotes: 4
Views: 5162
Reputation: 1
dialog = wx.DirDialog(None, "Navigate to Desktop directory of the version you require.", "~/", 0, (10, 10), wx.Size(400, 300)) dialog.CentreOnParent()
# Update directory with path selected in dialog
if dialog.ShowModal() == wx.ID_OK:
self.dir_path = dialog.GetPath()
self.updateDirectory()
It worked for me :D
Upvotes: 0
Reputation: 408
I have had the same problem with my own code and with the code posted above. However, I discovered that there is a way around it. When you use DirDialog, do not actually navigate into the directory you want to open. Instead, just highlight the directory you want to open and click open.
On Ubuntu 12.10, with Python 2.7.3, and wxPython 2.8.12.1 (and with 2.9), navigating all the way into a directory and clicking the open button just gives you your home directory. Navigating one directory up, highlighting the desired directory, and clicking the open button gives the correct result.
I have another machine running ubuntu 11.10 with Python 2.7.3 and I don't remember having this problem.
Clearly, this work around is not a proper solution but it's all I've been able to come up with so far. I hope it helps.
Upvotes: 3
Reputation: 4304
The dir dialogs in most operating systems are clumsy to use. You have to watch that little text box at the bottom and make sure it's populated correctly before clicking OK. I have a lot of users that have trouble with this. So maybe you are navigating to the folder you want, but you are not making the combination of clicks to get your choice into the text box. This may not be your problem, but I thought I'd mention it. Otherwise, you code looks fine to me.
good luck,
Mike
Upvotes: 0
Reputation: 33071
What OS are you using? Which Python? Which wxPython? This looks a lot like code from one of my tutorials, which worked fine for me. I went ahead and actually wrote a stripped-down runnable example from that tutorial:
import os
import wx
########################################################################
class MyForm(wx.Frame):
#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY,
"File and Folder Dialogs Tutorial")
panel = wx.Panel(self, wx.ID_ANY)
self.currentDirectory = os.getcwd()
dirDlgBtn = wx.Button(panel, label="Show DirDialog")
dirDlgBtn.Bind(wx.EVT_BUTTON, self.onDir)
#----------------------------------------------------------------------
def onDir(self, event):
"""
Show the DirDialog and print the user's choice to stdout
"""
dlg = wx.DirDialog(self, "Choose a directory:",
style=wx.DD_DEFAULT_STYLE
#| wx.DD_DIR_MUST_EXIST
#| wx.DD_CHANGE_DIR
)
if dlg.ShowModal() == wx.ID_OK:
print "You chose %s" % dlg.GetPath()
dlg.Destroy()
#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()
I ran this code on Windows 7 with Python 2.6.6 and wxPython 2.8.12.1. I selected three different directories and it printed all 3 different paths.
Upvotes: 6