Nov-13-2020, 06:19 PM
(This post was last modified: Nov-13-2020, 08:37 PM by deanhystad.)
Forget what I said earlier about the file dialog needing a callback. I am mixing up different GUI toolkits. In tkinter you treat a dialog like it waits for the file to be selected.
I would write the code so you don't access the file outside the on_openfile() function.
I would write the code so you don't access the file outside the on_openfile() function.
from tkinter import * from tkinter import filedialog def on_openfile(): pic = filedialog.askopenfilename() with open(pic) as f: print(f.read()) root = Tk() menubar = Menu(root) root.config(menu=menubar) file_menu = Menu(menubar) file_menu.add_command(label="Open", command=on_openfile) file_menu.add_command(label="Exit", command=root.destroy) menubar.add_cascade(label="File", menu=file_menu) root.mainloop()