2

I'm trying to make my python program more accessible with a minimal GUI with Tkinter grid.

I've created a class that creates a button that asks for a directory to be chosen. This works as it should. However, I want the path of the directory to be mirrored in a row below the button.

class dir_choice:
  def __init__(self, master, text, prow, pcol):
    self.dir_button = Button(master, text = text, command = self.askdir)
    self.dir_button.grid(row = prow, column = pcol)

    self.feedb = Label(master, text = "text")
    self.feedb.grid(row = prow+1, column = pcol)

  def askdir(self):
    name = askdirectory() #from tkFileDialog
    return name

In use:

dir1 = dir_choice(root, "FOLDER 1", 3, 2)

Instead of text = "text" I want to have something like "C:/user/A/B/C" to be shown below the button if that directory is chosen.

I think I am not correct assuming that this is in the name variable or if it is, I would need to define it differently. So my question is how I can access the value of the askdirectory() function and how I can push that value to the feedb label.

2
  • Quick note. self.dir_button and self.feedb are None by definition. The reason is that grid returns None. Don't chain grid like this. Call it in e separate line, i.e. self.dir_button.grid(...). Commented Jul 9, 2015 at 1:03
  • @Marcin Thanks. I changed this in my code and edited it. Any idea on my directory value problem? Commented Jul 9, 2015 at 1:09

1 Answer 1

4

It was probably just a copy-paste error, but the indentation is incorrect.

PEP-8 style calls for, among other things, classes to be named with CamelCase. It won't make your code run any faster, but readability counts.

Avoid chaining your geometry management methods. If you chain your geometry methods (e.g. grid()) onto the widget creation, you end up saving those methods' return value, which is always None, so self.dir_button and self.feedb are both just None. Instead, put the geometry management method on a separate line.

To change a widget's properties, simply configure() it (or config() for short). askdirectory() returns a string, so you can use it directly or save it to something like self.directory_name. It's not useful to return it anywhere because the function was called by a Button and the Button can't do anything with returned values.

class DirChoice:
    def __init__(self, master, text, prow, pcol):
        self.dir_button = Button(master, text = text, command = self.askdir)
        self.dir_button.grid(row = prow, column = pcol)
        self.feedb = Label(master, text = "text")
        self.feedb.grid(row = prow+1, column = pcol)
    def askdir(self):
        self.feedb.config(text= askdirectory()) #from tkFileDialog
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.