VictorBrandl
VictorBrandl

Reputation: 1

Use of askopenfilename in GUI

I am stuck with this issue on tkinter. I want to create a GUI that recover the path and the name of a file selected via askopenfilename and then will be utilized on subsequent codes. I tried may options but I did not succeed. The best I got is the follow but does not return what I need. Thanks for the help.

import tkinter as tk
from tkinter.filedialog import askopenfilename
class TkFileDialogExample(tk.Frame):

  def __init__(self, root):
    tk.Frame.__init__(self, root)
    self.a=[]
    tk.Button(self, text='askopenfilename', command=self.askopenfilename).pack()

  def askopenfilename(self):
      filename= askopenfilename()
      self.a.append(filename)
      return self.a

# MAIN PROGRAM
aa=[]
root = tk.Tk()
TkFileDialogExample(root).pack()
root.mainloop()
aa.append(TkFileDialogExample.askopenfilename)
print(aa)

Upvotes: 0

Views: 1971

Answers (2)

FIFO
FIFO

Reputation: 12

The code that follows creates a GUI that has a button on it that will pop up an askopenfilename dialog and adds the result to a list. The button also adds a label to the GUI that says on it the filepath that the askopenfilename dialog returns.

import tkinter as tk
from tkinter.filedialog import askopenfilename

###Step 1: Create The App Frame
class AppFrame(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        ###call the parent constructor
        tk.Frame.__init__(self, parent, *args, **kwargs)

        self.parent = parent

        ###Create button
        btn = tk.Button(self, text='askopenfilename',command=self.askopenfilename)
        btn.pack(pady=5)

    def askopenfilename(self):
        ###ask filepath
        filepath = askopenfilename()

        ###if you selected a file path
        if filepath:
            ###add it to the filepath list
            self.parent.filepaths.append(filepath)

            ###put it on the screen
            lbl = tk.Label(self, text=filepath)
            lbl.pack()

###Step 2: Creating The App
class App(tk.Tk):
    def __init__(self, *args, **kwargs):
        ###call the parent constructor
        tk.Tk.__init__(self, *args, **kwargs)

        ###create filepath list
        self.filepaths = []

        ###show app frame
        self.appFrame = AppFrame(self)
        self.appFrame.pack(side="top",fill="both",expand=True)


###Step 3: Bootstrap the app
def main():
    app = App()
    app.mainloop()

if __name__ == '__main__':
    main()

Upvotes: 0

Pythonista
Pythonista

Reputation: 11615

I think an example would be helpful here from the comment(s)

import tkinter as tk
from tkinter.filedialog import askopenfilename

filenames = []

def open_file():

    filename = askopenfilename()
    if filename:
        filenames.append(filename)

root = tk.Tk()
tk.Button(root, text='Open File', command=open_file).pack()
root.mainloop()
print(filenames)

When you exit the GUI you'll have a list of all valid opened filenames from the filedialog where the user did not click cancel.

Upvotes: 1

Related Questions