从 OptionMenu 执行命令

Executing a command from OptionMenu

有没有办法让选项菜单执行来自一系列不同命令的命令?

from tkinter import *
#creates 2 windows
root = Tk()
popup = Toplevel()

#places frame in popup window
popup_frame = Frame(popup)
popup_frame.pack()

#4 functions, text is just placeholder for idea
def popup1(popup):
    popup_frame.destroy()
    popup_frame = Frame(popup)
    label_popup1 = Label(popup_frame, text="unique textfor popup1")
    popup_frame.pack()
    label_popup1.pack()

def popup2(popup):
    popup_frame.destroy()
    popup_frame = Frame(popup)
    label_popup2 = Label(popup_frame, text="unique text for popup2")
    popup_frame.pack()
    label_popup2.pack()

def popup3(popup):
    popup_frame.destroy()
    popup_frame = Frame(popup)
    label_popup3 = Label(popup_frame, text="unique text for popup3")
    popup_frame.pack()
    label_popup3.pack()

def popup1(popup):
    popup_frame.destroy()
    popup_frame = Frame(popup)
    label_popup4 = Label(popup_frame, text="unique text for popup4")
    popup_frame.pack()
    label_popup4.pack()

#2 option menus offering different selections
options1 = ["popup1", "popup2"]
variable1 = StringVar()
drop_title1 = StringVar(root)
#sets initial name for widget
drop_title1.set("Please Choose Options 1 Or 2")
dropdownmenu1 = OptionMenu(root, drop_title1, *options, command=lambda:func())
dropdownmenu1.place(x=10, y=10)

options2 = ["popup3", "popup4"]
variable2 = StringVar()
drop_title2 = StringVar(root)
drop_title2 .set("Please Choose Options 3 Or 4")
dropdownmenu2 = OptionMenu(root, drop_title2, *options, command=lambda:func())
dropdownmenu2.place(x=100, y=10)

root.mainloop()

所以根框架中有 2 个不同的下拉菜单,如果您点击选项 1,popup_frame 将被删除并替换为选项 1 的内容,然后从第二个下拉菜单,选项 4 被选中,popup_frame 的内容变为 what option 4 contains?

OptionMenu 定义为 command 的函数将被赋予选定的值作为参数。因此,您可以定义回调函数来检查该值并根据它是哪个值调用另一个函数:

from tkinter import *
#creates 2 windows
root = Tk()
popup = Toplevel()

#places frame in popup window
popup_frame = Frame(popup)
popup_frame.pack()

#4 functions, text is just placeholder for idea
def popup1():
    global popup_frame
    popup_frame.destroy()
    popup_frame = Frame(popup)
    label_popup1 = Label(popup_frame, text="unique text for popup1")
    popup_frame.pack()
    label_popup1.pack()

def popup2():
    global popup_frame
    popup_frame.destroy()
    popup_frame = Frame(popup)
    label_popup2 = Label(popup_frame, text="unique text for popup2")
    popup_frame.pack()
    label_popup2.pack()

def popup3():
    global popup_frame
    popup_frame.destroy()
    popup_frame = Frame(popup)
    label_popup3 = Label(popup_frame, text="unique text for popup3")
    popup_frame.pack()
    label_popup3.pack()

def popup4():
    global popup_frame
    popup_frame.destroy()
    popup_frame = Frame(popup)
    label_popup4 = Label(popup_frame, text="unique text for popup4")
    popup_frame.pack()
    label_popup4.pack()

def func(val):
    if val == "popup1":
        popup1()
    elif val == "popup2":
        popup2()
    elif val == "popup3":
        popup3()
    elif val == "popup4":
        popup4()

#2 option menus offering different selections
options1 = ["popup1", "popup2"]
variable1 = StringVar()
drop_title1 = StringVar(root)
#sets initial name for widget
drop_title1.set("Please Choose Options 1 Or 2")
dropdownmenu1 = OptionMenu(root, drop_title1, *options1, command=func)
dropdownmenu1.pack(side=LEFT)

options2 = ["popup3", "popup4"]
variable2 = StringVar()
drop_title2 = StringVar(root)
drop_title2 .set("Please Choose Options 3 Or 4")
dropdownmenu2 = OptionMenu(root, drop_title2, *options2, command=func)
dropdownmenu2.pack(side=LEFT)

root.mainloop()