当我按下“更改”按钮时,如何在选项菜单中更改值?
这里是我到目前为止编写的代码:
import tkinter as tk
root = tk.Tk()
options =[
"eggs","meat","chicken",
"potato"
]
variable1 = tk.StringVar()
variable1.set(options[0])
om1 = tk.OptionMenu(root,variable1,*options)
om1.pack()
variable2 = tk.StringVar()
variable2.set(options[0])
om2 = tk.OptionMenu(root,variable2,*options)
om2.pack()
button_change = tk.Button(root,text="change")
button_change.pack()
root.mainloop()
请帮助...
OptionMenu
的关联变量交换它们的值:def swap_options():
# save the value of first OptionMenu
opt1 = variable1.get()
# set the value of first OptionMenu to that of second OptionMenu
variable1.set(variable2.get())
# set the value of second OptionMenu to the saved value of first OptionMenu
variable2.set(opt1)
button_change = tk.Button(root, text="change", command=swap_options)