I want to access a file for later use in Tkinter. The file is selected by the user and opened by a function. The function is called by a button. How do I access this file without calling the function again hence triggering the file selector again.
def select_file():
filetypes = (
('Excel files', '*.xlsx'),
)
filename = fd.askopenfilename(
title='Open file',
initialdir='/',
filetypes=filetypes)
showinfo(
title='Selected File',
message=filename
)
df = pd.read_excel(filename, engine='openpyxl')
return df
open_button = tk.Button(
root,
text='Select File',
command=select_file
)
open_button.pack()
df = select_file()
print(df.head())
I guess I could maybe make it a global variable within the function? Does not seem very elegant though. I must be missing something obvious. This is, as you might guess my first Tkinter project. Any and all help is appreciated