I have created functions that can display graphs and tables of csv files in a GUI I am making with tkinter.
I have a menubar
with an import button, a plot button, and a table button. The plot and table button can successfully plot graphs and tables of csv files respectively.
What I'd like to do, is when the user selects the import button, they select a file of their choice. Then, if they happen to select the plot button, the plot function works on the file they chose from import. Moreover, if they happen to select the table button, the table function works on the file they chose from import.
I have created a file opening function called openfile()
which remembers the name of the file opened.
The problem is that I don't know how to use menubar
and openfile()
such when the import button is clicked, my application stores the filename.
Any tips on how I would go about doing this?
Here's the code I've written for the menubar
and openfile()
:
def openfile():
name= askopenfilename()
return (name[19:])
class MyApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "MyApp")
# main frame
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
# creates the menubar at the top of the window
menubar = tk.Menu(container)
# import menu for importing csv files, initializes a file opening function (tbd)
filemenu = tk.Menu(menubar, tearoff=0)
filemenu.add_command(label="Import a CSV File", command = file_openerfunction)
menubar.add_cascade(label= "Import", menu=filemenu)
# plot menu for creating graphs and figures
Plot = tk.Menu(menubar, tearoff =0 )
Plot.add_command(label="Plot My CSV File", command= popupgraph)
menubar.add_cascade(label="Plot", menu=Plot)
# table menu for viewing data in a table
table = tk.Menu(menubar, tearoff = 0)
table.add_command(label="View MY CSV File", command = table)
table.add_cascade(label = "View Data", menu = table)
tk.Tk.config(self, menu=table)
....
....