0

Please advise on how to retrieve a file's full path into a variable after i pick one using tkinter

The whole idea of my GUI is to: 1. Have few buttions 2. Have address bar with file's full address

Once user clicks the button and picks the file >> file's path is displayed in the address bar as well as stored in a separate variable for future usage later in code

I've done some testing, but when checking for retrieved value - I get None.

def file_picker():
    """Pick enova .xlsx file"""
    path = filedialog.askopenfilename(filetypes=(('Excel Documents', '*.xlsx'), ('All Files', '*.*')))
    return path

file_button = tkinter.Button(root, text='Users File', width=20, height=3, 
bg='white', command=custom_functions.file_picker).place(x=30, y=50)

Apart form that I found another code snippet, but this simply captures line onto the GUI interface, not saving file path in any variable though:

def browsefunc():
    filename = filedialog.askopenfilename()
    pathlabel.config(text=filename)
    print(pathlabel)


browsebutton = tkinter.Button(root, text="Browse", command=browsefunc).pack()

pathlabel = tkinter.Label(root).pack()

Expected result: https://i.stack.imgur.com/c9pcz.jpg - unfortunatelly I cannot post images yet so uploaded one onto imgur

Danil Tk
  • 29
  • 1
  • 6

1 Answers1

0

To capture the full path of a file using Tkinter you can do the following. The output of your full file path will be displayed in the "Entry" field / your address bar as per your requirement in your original post.

Update

import tkinter
from tkinter import ttk, StringVar
from tkinter.filedialog import askopenfilename

class GUI:

    def __init__(self, window): 
        # 'StringVar()' is used to get the instance of input field
        self.input_text = StringVar()
        self.input_text1 = StringVar()
        self.path = ''
        self.path1 = ''

        window.title("Request Notifier")
        window.resizable(0, 0) # this prevents from resizing the window
        window.geometry("700x300")

        ttk.Button(window, text = "Users File", command = lambda: self.set_path_users_field()).grid(row = 0, ipadx=5, ipady=15) # this is placed in 0 0
        ttk.Entry(window, textvariable = self.input_text, width = 70).grid( row = 0, column = 1, ipadx=1, ipady=1) # this is placed in 0 1

        ttk.Button(window, text = "Enova File", command = lambda: self.set_path_Enova_field()).grid(row = 1, ipadx=5, ipady=15) # this is placed in 0 0
        ttk.Entry(window, textvariable = self.input_text1, width = 70).grid( row = 1, column = 1, ipadx=1, ipady=1) # this is placed in 0 1

        ttk.Button(window, text = "Send Notifications").grid(row = 2, ipadx=5, ipady=15) # this is placed in 0 0

    def set_path_users_field(self):
        self.path = askopenfilename() 
        self.input_text.set(self.path)

    def set_path_Enova_field(self):
        self.path1 = askopenfilename()
        self.input_text1.set(self.path1)

    def get_user_path(self): 
        """ Function provides the Users full file path."""
        return self.path

    def get_enova_path1(self):
        """Function provides the Enova full file path."""
        return self.path1


if __name__ == '__main__':
    window = tkinter.Tk()
    gui = GUI(window)
    window.mainloop()
    # Extracting the full file path for re-use. Two ways to accomplish this task is below. 
    print(gui.path, '\n', gui.path1) 
    print(gui.get_user_path(), '\n', gui.get_enova_path1())

Note: I added a comment to point you to where the full file path is stored, in my example it's 'path' & 'path1'.

  • @Danil Tk - Does this help you? –  Jan 20 '19 at 17:44
  • unfortunately it is not working. https://imgur.com/a/NbiOPzG here's the example I want it to look like. So when I click button - the address appears in my GUI + is saved in a variable so I could later use it – Danil Tk Jan 21 '19 at 10:29
  • What happens when you run my example? When I run it a Windows dialogue box opens up and I can select a file. After selecting a file the full path to that file is displayed in the output of my IDE. This I have done to show that the full path is stored in a variable, in my example a variable I have labeled as 'path'. Are you requesting for code that provides the GUI interface like the example you provided ? –  Jan 21 '19 at 13:43
  • correct. Your example withdraws the whole gui box, yet i want it to be available and button to return me the address of the file I pick – Danil Tk Jan 21 '19 at 14:37
  • @Danil Tk - Please run the updated code. It will now provide a GUI having three buttons and two fields, click the button, choose a file and the full file path will be populated in the fields. Similar to your example. –  Jan 21 '19 at 23:16