如何在optionmen tkinter python中交换值?

问题描述 投票:0回答:1

当我按下“更改”按钮时,如何在选项菜单中更改值?

这里是我到目前为止编写的代码:

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()

请帮助...

python button tkinter root optionmenu
1个回答
0
投票
您可以通过两个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)

© www.soinside.com 2019 - 2024. All rights reserved.