0

The only real material I have found to help me with this is this SO post and it has gotten me to this point.

I am trying to basically get the user to click the "Browse" button, select a directory when the tkFileDialog.askdirectory window appears, and when the user selects the appropriate directory - the output is placed inside a Tkinter Entry widget.

Below is the code I am using currently. Right now, it throws a NameError: global name 'var' is not defined when submitting a directory within the tkFileDialog.askdirectorywindow.

I am still learning Python, slowly, and decided that I would try and use OOP. Hence why I am getting really confused right now and the code is messy / hacky.

class MainMenuFrames:
    def __init__(self, master):
        self.TitleFrame = ttk.Frame(master)
        self.OptionsLabelFrame = ttk.LabelFrame(master, text="Options")

        self.TitleFrame.pack(side=TOP,
                             fill="both",
                             expand=True)
        self.OptionsLabelFrame.pack(padx=OptionFramePadding,
                                    pady=OptionFramePadding)    

class OptionsContent:
    def __init__(self, bottomframe):
        def askdirectory():
            dirname = tkFileDialog.askdirectory(**self.dir_opt)
            if dirname:
                var.set(dirname)
                return var

        self.dir_opt = options = {}
        options["initialdir"] = "C:\\"
        options["mustexist"] = False
        options["parent"] = root

        def UserFileInput(status):
            text = status
            var = StringVar(root)
            var.set(text)
            w = Entry(bottomframe, width=27, textvariable=var)
            w.grid(row=1, column=2, padx=(5, 5), columnspan=2, sticky=E)
            return w, var

        SVNPathEntry = UserFileInput("")


        SVNBrowseButton = ttk.Button(bottomframe,
                                     text="Browse...",
                                     command=askdirectory)

        SVNBrowseButton.grid(row=2, column=3, padx=(0, 5), sticky=E)


MainMenu = MainMenuFrames(root)
Title = TitleLabel(MainMenu.TitleFrame)
Options = OptionsContent(MainMenu.OptionsLabelFrame)

Error in question is within the if statement var.set(dirname)

Goralight
  • 2,067
  • 6
  • 25
  • 40
  • 1
    I think your definition of `var` on line 77 and the definition of your entry widget on line 79 should be in the `__init__` method of `OptionsContent` – Kidus May 22 '17 at 16:19
  • You have defined `var` within `UserFileInput`, therefore your `askdirectory` function cannot see it. Try declaring `var` within `__init__` using `self`. – Cov May 22 '17 at 16:20
  • Please reduce the code down as much as possible. There's a lot of code there that is unrelated to your problem. See [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) – Bryan Oakley May 22 '17 at 18:45
  • @BryanOakley - Updated with more concise code. As for the other comments I tried the fixes you suggested but it did not work :( – Goralight May 22 '17 at 19:36
  • 2
    @Kidus I just placed `var = StringVar(root)` within the `__init__` of the class and it worked! It was a long day yesterday and was really at my wits end! I wish I could give you some rep or something but your advice did help me, thank you! – Goralight May 23 '17 at 08:19

0 Answers0