WxPython Dirdialog always returns the same directory

I'm starting to learn wxPython to create graphical applications, and I have to face some problems: when using wx.DirDialog to select a folder, no matter which folder I selected, dlg.GetPath () always returns the same folder that in my case: / home / loai

my bind function is as follows:

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() 

it always prints: you chose / home / loai

thanks

+4
source share
4 answers

I had the same problem with my own code and with the code above. However, I found that there is a way around. When you use DirDialog, do not go to the directory you want to open. Instead, just highlight the directory you want to open and click "Open."

In Ubuntu 12.10 with Python 2.7.3 and wxPython 2.8.12.1 (and from 2.9), completely moving to the directory and clicking the "Open" button, you simply get your home directory. Moving one directory up, highlighting the desired directory and pressing the open button gives the correct result.

I have another machine with Ubuntu 11.10 with Python 2.7.3, and I don't remember this problem.

It is clear that this work is not the right solution, but all that I could still find. Hope this helps.

+3
source

What OS are you using? Which Python? Which wxPython? This is very similar to the code from one of my tutorials , which works great for me. I went ahead and actually wrote the lesson a stylized example:

 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 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 using Python 2.6.6 and wxPython 2.8.12.1. I selected three different directories and printed all three different paths.

+6
source

The dir dialogs on most operating systems are inconvenient to use. You should look at this small text box below and make sure it is filled out correctly before clicking OK. I have many users who have problems with this. Therefore, perhaps you go to the folder you want, but you do not do a combination of clicks to get your choice in the text box. This may not be your problem, but I thought I would say that. Otherwise, the code looks good to me.

good luck

Mike

0
source

Try the following:

dialog = wx.DirDialog (No, "Go to the Desktop directory of the version you want", "~ /", 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

0
source

All Articles