I'm creating a tkinter GUI for the purposes of opening and analyzing an EDF file named "raw" using the mne package. Currently, when I run the script, named "my_script.py", it first prompts me to open a file using the following code:
window = Tk()
window.filename = filedialog.askopenfilename(initialdir = "Users/fishbacp/Desktop",title =
"Select file",filetypes = (("EDF files","*.edf"),("all files","*.*")))
raw = mn.io.read_raw_edf(window.filename, preload=True)
# Now perform my first operation on the returned file, "raw":
data,times=raw[:,:]
# In subsequent lines I fill my gui with checkboxes, input boxes, labels etc. to perform more tasks.
This works fine, but the file dialog starts before I see the various widgets in my window.
Now I want to run the module so that instead of the file dialog starting automatically, I first see the GUI and then use a button in it to run the file dialog. Here's my attempt:
window = Tk()
def open_file():
window.filename = filedialog.askopenfilename(initialdir = "Users/fishbacp/Desktop",title
= "Select file",filetypes = (("EDF files","*.edf"),("all files","*.*")))
raw = mn.io.read_raw_edf(window.filename, preload=True)
return raw
open_file_button=Button(window, bg='lightgray',text="Select
File",command=open_file)
open_file_button.grid(row=0,column=0,padx=10, pady=10)
# Now perform my first operation on the returned file, "raw":
data,times=raw[:,:]
The error message states, "Traceback (most recent call last): File "/Users/fishbacp/Desktop/my_script.py", line 67, in data,times=raw[:,:] NameError: name 'raw' is not defined
So, I'm missing something basic about how tkinter works in terms of what I should be doing to get my open_file function return the raw file as I want.