1

I'm using tkinter to create a GUI for an old script I have, but I'm stuck right now. I want to click a button to open the "Search File" window, choose a specific file and save its path in a variable. The code I have is able to open the window, then I can select the file and display it's path, but I couldn't find a way to save this path in a variable. This is what I have:

from tkinter import *
from tkinter import filedialog

def get_file_path():
    # Open and return file path
    file_path= filedialog.askopenfilename(title = "Select A File", filetypes = (("mov files", "*.png"), ("mp4", "*.mp4"), ("wmv", "*.wmv"), ("avi", "*.avi")))
    l1 = Label(window, text = "File path: " + file_path).pack()

window = Tk()
# Creating a button to search the file
b1 = Button(window, text = "Open File", command = get_file_path).pack()
window.mainloop()

Does anyone know a good way to do this?

1
  • Your function doesn't return anything. Using a global is also an option. Commented Apr 19, 2020 at 23:31

2 Answers 2

2

Use global variable:

from tkinter import *
from tkinter import filedialog

def get_file_path():
    global file_path
    # Open and return file path
    file_path= filedialog.askopenfilename(title = "Select A File", filetypes = (("mov files", "*.png"), ("mp4", "*.mp4"), ("wmv", "*.wmv"), ("avi", "*.avi")))
    l1 = Label(window, text = "File path: " + file_path).pack()

window = Tk()
# Creating a button to search the file
b1 = Button(window, text = "Open File", command = get_file_path).pack()
window.mainloop()
print(file_path)

Once your windows closed, you'll get your file_path.

Sign up to request clarification or add additional context in comments.

1 Comment

This does work. However, if I want to use "file_path" as the parameter for other function, it's returned that it is still not defined. I have this issue if I use l4 = Label(window, text = "This video has " + str(count_frames(file_path)) + " frames.") under b1 = Button(window, text = "Open File", command = get_file_path).pack() (count_frames returns the number of frames of a video in a given file path). How can the code be improved to avoid this?
1

You could declare file_path as global variable in get_file_path

def get_file_path():
    global file_path
    # Open and return file path
    file_path= filedialog.askopenfilename(title = "Select A File", filetypes = (("mov files", "*.png"), ("mp4", "*.mp4"), ("wmv", "*.wmv"), ("avi", "*.avi")))
    l1 = Label(window, text = "File path: " + file_path).pack()

Then you can access that variable from anywhere in the script

------Edit------ According to what you said in your comment, i say you could use tkinter.StringVar to save the file path, and then to access it later when calling count_frames as an argument.

It could be implemented as the following:

from tkinter import *
from tkinter import filedialog

file_path_var = StringVar()

def get_file_path():
    # Open and return file path
    file_path= filedialog.askopenfilename(title = "Select A File", filetypes = (("mov files", "*.png"), ("mp4", "*.mp4"), ("wmv", "*.wmv"), ("avi", "*.avi")))
    file_path_var.set(file_path) #setting the variable to the value from file path
    #Now the file_path can be acessed from inside the function and outside
    file_path_var.get() # will return the value stored in file_path_var
    l1 = Label(window, text = "File path: " + file_path).pack()

window = Tk()
# Creating a button to search the file
b1 = Button(window, text = "Open File", command = get_file_path).pack()
# will also return the value saved in file_path_var
file_path_var.get()
window.mainloop()
print(file_path)

So now where ever your count_frames function is, as long as you have selcted a file first you should be able to just do

count_frames(file_path_var.get())

1 Comment

Hey Jojo As I said to Panda50, this does work. However, if I try to use this global variable in as a parameter of another function, I still get the same problem. If I use l4 = Label(window, text = "This video has " + str(count_frames(file_path)) + " frames.") , it still doesnt work (count_frames returns the number of frames of a video in a given file path).

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.