Reputation: 73
I was wondering how I could change button behavior based on which item in the drop down menu I have selected in the "Check Maintenance" tab. For example, when I have oil selected, I want the button to perform the check_oil_change() function. When it has spark plugs selected, I want the button to perform a different function that will handle that specific maintenance. (sorry I had to rewrite my question)
from tkinter import *
from tkinter import ttk
#initializing root window
root = Tk()
root.title("Car Maintenance App")
root.resizable(False, False)
tab_control = ttk.Notebook(root)
tab1 = ttk.Frame(tab_control)
tab2 = ttk.Frame(tab_control)
tab_control.add(tab1, text="History")
tab_control.add(tab2, text="Check maintenance")
tab_control.pack(expand=1, fill="both")
#functions
miles = IntVar()
last_miles = IntVar()
miles_between_changes = 3000
def check_oil_change():
miles = miles_entry.get()
miles = int(miles)
last_miles = last_miles_entry.get()
last_miles = int(last_miles)
miles_till_oilchange = miles_between_changes - (miles - last_miles)
if miles_till_oilchange == 0:
output_label.config(text="You are due for an oil change")
if miles_till_oilchange > 0:
output_label.config(text="You have {} miles until next oil change.".format(miles_till_oilchange))
if miles_till_oilchange < 0:
output_label.config(text="You are over due {} miles for an oil change.".format(abs(miles_till_oilchange)))
#tab1 widgets
last_miles_label = ttk.Label(tab1, text= "How many miles was your last oil change at?")
last_miles_entry = ttk.Entry(tab1, width=7)
#tab1 positioning
last_miles_label.grid(row=0, column=0)
last_miles_entry.grid(row=0, column=1)
#tab2 widgets
miles_label = ttk.Label(tab2, text= "Enter your cars current mileage:")
miles_entry = ttk.Entry(tab2, width=7)
miles_button = ttk.Button(tab2, text="Enter", command=check_oil_change)
output_label = ttk.Label(tab2, text="")
maintenance_choices = [ #drop down menu options
"Oil change",
"Tire Rotation",
"Spark Plugs",
]
clicked = StringVar()
clicked.set( "Oil" )
maintenance_choices_dropdown = OptionMenu(tab2, clicked, *maintenance_choices)
#tab2 positioning
maintenance_choices_dropdown.grid(row=0, column=0, sticky="W")
miles_label.grid(row=1, column=0)
miles_entry.grid(row=1, column=1)
miles_button.grid(row=1, column=2)
output_label.grid(row=2, column=1)
root.mainloop()
Upvotes: 1
Views: 946
Reputation: 118
I added a command attribute to Options Menu. Now, whenever an option is selected from the menu, it triggers the OptionMenu_SelectionEvent
function. Now, within this function, you can change the command of the button which it triggers to.
from tkinter import *
from tkinter import ttk
#initializing root window
root = Tk()
root.title("Car Maintenance App")
root.resizable(False, False)
tab_control = ttk.Notebook(root)
tab1 = ttk.Frame(tab_control)
tab2 = ttk.Frame(tab_control)
tab_control.add(tab1, text="History")
tab_control.add(tab2, text="Check maintenance")
tab_control.pack(expand=1, fill="both")
#functions
miles = IntVar()
last_miles = IntVar()
miles_between_changes = 3000
def check_oil_change():
miles = miles_entry.get()
miles = int(miles)
last_miles = last_miles_entry.get()
last_miles = int(last_miles)
miles_till_oilchange = miles_between_changes - (miles - last_miles)
if miles_till_oilchange == 0:
output_label.config(text="You are due for an oil change")
if miles_till_oilchange > 0:
output_label.config(text="You have {} miles until next oil change.".format(miles_till_oilchange))
if miles_till_oilchange < 0:
output_label.config(text="You are over due {} miles for an oil change.".format(abs(miles_till_oilchange)))
def test():
print("Here")
def OptionMenu_SelectionEvent(value):
if value == 'Oil change':
miles_button['command']=test
elif value == 'Tire Rotation':
pass
elif value == 'Spark Plugs':
pass
#tab1 widgets
last_miles_label = ttk.Label(tab1, text= "How many miles was your last oil change at?")
last_miles_entry = ttk.Entry(tab1, width=7)
#tab1 positioning
last_miles_label.grid(row=0, column=0)
last_miles_entry.grid(row=0, column=1)
#tab2 widgets
miles_label = ttk.Label(tab2, text= "Enter your cars current mileage:")
miles_entry = ttk.Entry(tab2, width=7)
miles_button = ttk.Button(tab2, text="Enter", command=check_oil_change)
output_label = ttk.Label(tab2, text="")
maintenance_choices = [ #drop down menu options
"Oil change",
"Tire Rotation",
"Spark Plugs",
]
clicked = StringVar()
clicked.set( "Oil" )
maintenance_choices_dropdown = OptionMenu(tab2, clicked, *maintenance_choices, command = OptionMenu_SelectionEvent)
#tab2 positioning
maintenance_choices_dropdown.grid(row=0, column=0, sticky="W")
miles_label.grid(row=1, column=0)
miles_entry.grid(row=1, column=1)
miles_button.grid(row=1, column=2)
output_label.grid(row=2, column=1)
root.mainloop()
Upvotes: 1