有没有办法让选项菜单从一系列不同的命令中执行命令?
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的内容更改为选项4包含的内容?
答案 0 :(得分:1)
command
定义为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():
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()