Hello Python community,
I have a basic question about the wx.DirDialog method/event handler system and how it works. I had a problem today, but solved it. Basically what I'm trying to do is have a frame, then use event handler wx.DirDialog to open up another dialogue where I can choose the filepath, and then I would like this filepath to populate into the text control box. I have solved the problem, but I don't understand the logic behind it, even though it works. :icon_neutral: :icon_lol:
Basically my problem was similar to this previous post:
http://www.daniweb.com/forums/thread97220.html#
Also listed here: http://mail.python.org/pipermail/tutor/2007-November/058662.html
So basically we're using the wx.DirDialog method on event call. YOu can see the basic functionality of this feature by looking at pictures;
"wxDirDialog1.jpg" and "wxDirDialog3.jpg"
This is the main snippet of the code (there's more, but this is all we need for illustration purposes).
This is what I wrote in the beginning and when ever I opened the 'get directory' dialogue from the event handler via the wx.DirDialog method I would be able to select the new file path but the full path directory would never show up in the text control box:
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,'Get Quotes Tool', size=(400,300))
self.SetIcon(wx.Icon('C:\Basic_Icons\globe_32.png',wx.BITMAP_TYPE_PNG))
panel=wx.Panel(self)
status=self.CreateStatusBar()
basicLabel=wx.StaticText(panel,-1,"Folder:",(52,65))
[B]FilePathTextBox[/B]=wx.TextCtrl(panel,-1,"",size=(200,-1),pos=(100,60))
self.button = wx.Button(panel,-1,"Browse",pos=(305,60))
self.Bind(wx.EVT_BUTTON, self.OnClick, self.button)
def OnClick(self,event):
dialog = wx.DirDialog(None, "Choose a directory:",
style=wx.DD_DEFAULT_STYLE | wx.DD_CHANGE_DIR)
if dialog.ShowModal() == wx.ID_OK:
self.SetStatusText('You selected: %s\n' %dialog.GetPath())
[B]dirname=dialog.GetPath()
FilePathTextBox.write(dirname)[/B]
dialog.Destroy
I would usually get the following error with the above code:
FilePathTextBox.SetValue(dirname)
NameError: global name 'FilePathTextBox' is not defined
See the image below: "globalnamenotdefined.jpg"
Then after banging my head on the problem for a couple of hours, and finding some help on the web, I tried the following code:
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,'Get Quotes Tool', size=(400,300))
self.SetIcon(wx.Icon('C:\Basic_Icons\globe_32.png',wx.BITMAP_TYPE_PNG))
panel=wx.Panel(self)
status=self.CreateStatusBar()
basicLabel=wx.StaticText(panel,-1,"Folder:",(52,65))
[B]self.FilePathTextBox[/B]=wx.TextCtrl(panel,-1,"",size=(200,-1),pos=(100,60))
self.button = wx.Button(panel,-1,"Browse",pos=(305,60))
self.Bind(wx.EVT_BUTTON, self.OnClick, self.button)
def OnClick(self,event):
dialog = wx.DirDialog(None, "Choose a directory:",
style=wx.DD_DEFAULT_STYLE | wx.DD_CHANGE_DIR)
if dialog.ShowModal() == wx.ID_OK:
self.SetStatusText('You selected: %s\n' %dialog.GetPath())
[B]self.dirname=dialog.GetPath()
self.FilePathTextBox.write(self.dirname)[/B]
dialog.Destroy
You can see that it now works if you look at the picture "wxDirDialog2.jpg"
I made three adjustments to my original code:
1. FilePathTextBox=wx.TextCtrl(panel,-1,"",size=(200,-1),pos=(100,60)) ---> I changed to ---> self.FilePathTextBox=wx.TextCtrl(panel,-1,"",size=(200,-1),pos=(100,60))
On the event handler OnClick() I did the following:
2. dirname=dialog.GetPath() ---> changed to ---> self.dirname=dialog.GetPath()
self.FilePathTextBox.write(dirname)
3. FilePathTextBox.write(dirname) ----> changed to ----> self.FilePathTextBox.write(dirname)
As you can see the main thing I did was changed those 3 lines by putting ".self" in front of them.
Does anyone know why doing this solved my problem and why I didn't get the "NameError: global name 'FilePathTextBox' is not defined" problem anymore??
It seems as if the ".self" feature references objects within the class or something, and not putting that ".self" in front of the lines means that I'm accessing global variables when it is not impossible b/c they are local functions.
However ... why is it that putting ".self" in front of the lines makes it okay for a local variable to access global instances??
Any help on this from someone knowledgeable about the core language would be great. I would like to understand the core logic behind why putting .self in front of everything works, and what this means and all.
This is wxPython btw.
Thanks in advance.