0

I am using; Python 3.4, Windows 8, tkinter. I am trying to create a generic browse button that will get a file name and assign it to a variable.

I have created the following code to do this.

from tkinter import *
from tkinter import filedialog
from tkinter import ttk

class Application(Frame):
# A GUI Application.
# Initialize the Frame
    def __init__(self, master):
        Frame.__init__(self, master)
        nbook = ttk.Notebook(root)
        nbook.pack(fill='both', expand='yes')
        f1 = ttk.Frame(nbook)
        nbook.add(f1, text='QC1')
        self.qc1_tab(f1)

# create QC1 tab contents
    def qc1_tab(self, tab_loc):

        # Set up file name entry.
        Label(tab_loc, text="Select file:").grid(pady=v_pad, row=0, column=0, sticky=W)
        self.flnm = ttk.Entry(tab_loc, width=60)
        self.flnm.focus_set()
        self.flnm.grid(pady=v_pad, row=0, column=1, columnspan=2, sticky=W)
        ttk.Button(tab_loc, text="Browse...", width=10, command=self.browse).grid(row=0, column=3)

    def browse(self):
        temp = filedialog.askopenfilename()
        self.flnm.delete(0, END)
        self.flnm.insert(0, temp)

root = Tk()
app = Application(root)
root.mainloop()

The only problem with this is that the browse button is tied to self.flnm and cannot be used for anything else. I plan to use the browse button several times to acquire the file name of several different files and would rather not have multiple browse commands.

I need to call it from a button and somehow assign it to a variable afterwards.

I was thinking of something like

ttk.Button(..., command=lambda: self.flnm = self.browse)
...
def browse(self):
    filename = filedialog.askopenfilename()
    return filename

but that failed terribly.

How can I make a general purpose browse button?

3
  • I don't know too much about TKinter, but I can see at least one issue: you can't do assignment operations inside a lambda. Instead, use another named function for that. Commented Jul 21, 2015 at 23:25
  • I am not sure how you want to choose your files, but which variable you want to assign the file name to? You say you might want to choose different file names in the course of your application, how do you want to manage this? Do you want for example to override the previous chosen file name, or you want to keep track of all file names that the user might choose during the application's life? These details are important to us in order to give a specific answer. Commented Jul 22, 2015 at 0:40
  • @nbro. I want to assign it to self.flnm. I want to assign the file name to an outside variable that I can later use whenever I need it. Eric Levieil was able to provide sufficient code for what I need. Commented Jul 22, 2015 at 14:07

1 Answer 1

1

You can write:

def browse(self, target):
    temp = filedialog.askopenfilename()
    target.delete(0, END)
    target.insert(0, temp)

ttk.Button(..., command=lambda: self.browse(self.flnm))
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.