简体   繁体   English

Tkinter:实现下拉菜单

[英]Tkinter: implementing drop down menu

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.例如,当我选择了油时,我希望按钮执行 check_oil_change() 函数。 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()

I added a command attribute to Options Menu.我在选项菜单中添加了一个命令属性。 Now, whenever an option is selected from the menu, it triggers the OptionMenu_SelectionEvent function.现在,无论何时从菜单中选择一个选项,它都会触发OptionMenu_SelectionEvent函数。 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM