PDA

View Full Version : tkinter askdirectory question



dalandtwist
July 20th, 2009, 09:28 AM
Hello,

I need to get the name of a directory and place that name into a label widget.

Currently I am using the askdirectory function to open the browser and select a folder. like below and that seems to work no prob.

def Selectjob(self):
dir1 = askdirectory(title="Select the Job Folder", mustexist=1)

What I need to figure out is how to insert that dir1 value (the directory path) into a
label. What I have below is not working. I am pretty new at this, and feel like I have somekind of syntax error.

self.lbl = Label(self, text="dir1.get()" )
self.lbl.grid(row=3, column=5)

Any ideas?

thanks!

david

smartbei
July 20th, 2009, 09:38 AM
Just take it out of the quotes:


self.lbl = Label(self, text=dir1.get())
self.lbl.grid(row=3, column=5)

Does that work?

EDIT: more info:
To change it after it has been created check out the reference here http://effbot.org/tkinterbook/label.htm.

dalandtwist
July 20th, 2009, 10:29 PM
Thank you for replying. I tried removing the quotes but no luck. I have attached my script. It's a simple folder building tool but I need the ability to choose a directory so I can add additional folders when needed. Any help is appreciated. I am new to Tkinter and Python but am very interested in learning. I commented out the area where I am having problems.

thank you all,

david

stevescripts
July 21st, 2009, 03:19 PM
dalandtwist,

No time right now to look at your script.

A quickie hack that might help with your problem:



#!/usr/bin/python

from Tkinter import *
from tkFileDialog import *

def the_dir():
dirname = askdirectory()
label.configure(text = dirname)

main = Tk()

button = Button(main, text = "Choose Directory", command = the_dir)
label = Label(main)

button.pack()
label.pack()

main.mainloop()


If this doesn't help, will try to find time to look at your script.

Steve

dalandtwist
July 21st, 2009, 08:43 PM
hi steve,

I tried you code and it works fine on it's own but I get errors when I try to implement.
Maybe the problem is because I am using grid. Not sure. Would appreciate any pointers.

thank you all,

david

stevescripts
July 21st, 2009, 08:59 PM
david,

grid is *NOT* the problem - I took a quick look at your script, and still don't have time right now to help you clean it up a bit. (it appears to be a scope problem, on a quick glance) Maybe tommorrow...

Steve

stevescripts
July 22nd, 2009, 03:38 PM
hmm ... I thought I had it fixed, but I don't...

I find your widget naming ... unusual ... and the indentation and program flow a bit confusing.

Maybe I can find some more time later. Gotta pay attention to paywork.

Steve

smartbei
July 22nd, 2009, 07:37 PM
If you want the user to select the directory before he even sees the gui, change the part you are confused about to this:


self.Selectjob()
self.lbl = Label(self, text=self.dirshotname)
self.lbl.grid(row=3, column=5)

That works for me, but is really unintuitive.

I think what you really want is that if the user clicks on "Click to Select Job", to THEN set the label. If so, change the problematic code to:


self.lbl_string = StringVar()
self.lbl = Label(self, textvariable = self.lbl_string)
self.lbl.grid(row=3, column=5)

and change the method Selectjob to:


def Selectjob(self):
self.lbl_string.set(tkFileDialog.askdirectory())


Note that askdirectory returns either a string with the directory path, or None on failure (user canceled, etc.). Make sure to add error checking code to deal with the case of None.

Also, note that your code is really mess with its indentation; there are spaces and tabs mixed...yech. Best Practice for python is considered 4 spaces per indentation level. Lastly, you don't really need to keep every single one of your GUI elements as members of the class, and if you do, then make sure not to override them (like you do (probably accidentaly due to copy/paste) with the labels).

stevescripts
July 22nd, 2009, 08:11 PM
smartbei - Glad you found the time I havent been able to make.

david - make sure you understand what smartbei is saying about your code (and the overriding of the label names)

EDIT: It seems to me, that it might be a good idea to use separate frames for each section of you GUI to help reduce possible user confusion

Steve

dalandtwist
July 22nd, 2009, 09:41 PM
Steve, Smartbei,

Thank you very much for your insight, I am brand new at this and will take your comments and work on it tonight. I am grateful for your time.

-david

smartbei
July 22nd, 2009, 09:43 PM
No problem - we are always happy to help, and generally I find that I learn something new every time :-). (In this case, StringVar was a new one for me)