I'm making a simple gui that has three buttons:
- select a file path to file
- select a file path to a separate file
- run a function, passing the file paths selected from buttons 1 and 2.
I have this so far:
import tkinter as tk
from tkinter.filedialog import askopenfilename
import pathlib
root = tk.Tk()
class App:
def __init__(self, main):
myFrame = tk.Frame(main)
myFrame.pack()
self.btn_1= tk.Button(main, text="Choose file", command=self.choose_data).pack()
self.btn_2= tk.Button(main, text="Choose file", command=self.choose_data).pack()
self.run_button = tk.Button(main, text="Run", command=lambda: self.run_analysis(self.btn_1, self.btn_2)).pack()
def choose_data(self):
global data_file
data_file = pathlib.Path(askopenfilename(filetypes=[("Excel files", ".xls .xlsx")]))
return data_file
def run_analysis(self,a,b):
do_something(a,b)
a = App(root)
root.mainloop()
The first buttons work as expected. And I can see the filepath is stored in a variable that I can print from the choose_data function
When I try to use these attributes from the run function I get this error
ValueError: Invalid file path or buffer object type: <class 'NoneType'>
When I print the attributes from a function within the init method, they are None. So from that and the error, I know thats where I'm going wrong.
I think its because the init method is beng called before the attributes are defined? And so when I call the run_analysis method it does not work?
I tried making data_file a global variable so that it would allow me to use it anywhere, but thats not working for me.
Any help would be appreciated
Thanks
choose datafunction. The same error persists. I have updated the question.returnkeyword isn't a magic fix; you need to actually use functions correctly. Also, yourself.btn_1andself.btn_2are saved as justNoneas well.