Reputation: 1263
I'm writing a simple script using tkinter that allows the user to load a csv file into a dataframe via a menu option. Once it's selected, the contents are printed to the screen. Then another option under the same menu plots the dataframe. I'm confused as to how I go about disabling the plot option until the dataframe is actually loaded. My example thus far:
import pandas as pd
from matplotlib import pyplot as plt
from tkinter import Tk, filedialog,Menu
root = Tk()
def _open():
root.filename = filedialog.askopenfilename(initialdir = "Users/fishbacp/Desktop",title = "Select file",filetypes = (("csv","*.csv"),("all files","*.*")))
df=pd.read_csv(root.filename)
print(df)
def _plotdf():
df.plot()
plt.show()
def _quit():
root.quit()
root.destroy()
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Open", command=_open)
filemenu.add_command(label="Plot", command=_plotdf)
filemenu.add_command(label="Exit", command=_quit)
menubar.add_cascade(label="Options", menu=filemenu)
root.config(menu=menubar)
root.mainloop()
I realize
filemenu.entryconfig('Plot',state='disabled')
will disable the plot button. But I'm confused as to whether it could be incorporated for my purposes.
Upvotes: 1
Views: 918
Reputation:
You could try this. Since the print function is called only after dataframe is loaded, the menu option are enabled and disabled after it.
import pandas as pd
from matplotlib import pyplot as plt
from tkinter import *
from tkinter import filedialog
root = Tk()
def _open():
root.filename = filedialog.askopenfilename(initialdir = "Users/fishbacp/Desktop",title = "Select file",filetypes = (("csv","*.csv"),("all files","*.*")))
global df
df=pd.read_csv(root.filename)
print(df)
filemenu.entryconfig("Open",state="disabled")#==Disable Menu Option
filemenu.entryconfig("Plot",state="normal")#==Enable Menu Option
def _plotdf():
df.plot()
plt.show()
filemenu.entryconfig("Plot",state="disabled")#==Disable Menu Option
filemenu.entryconfig("Open",state="normal")#==Enable Menu Option
def _quit():
root.quit()
root.destroy()
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Open", command=_open)
filemenu.add_command(label="Plot", command=_plotdf)
filemenu.entryconfig("Plot",state="disabled") #==Disable Menu Option
filemenu.add_command(label="Exit", command=_quit)
menubar.add_cascade(label="Options", menu=filemenu)
root.config(menu=menubar)
root.mainloop()
Upvotes: 4