📜  更改所选选项 optionmenu tkinter - Python 代码示例

📅  最后修改于: 2022-03-11 14:47:24.389000             🧑  作者: Mango

代码示例1
"""
Change selected option optionmenu tkinter.
If at any time you change the value of the variable, it will update your widget.
"""

import tkinter as tk # First we import tkinter as tk

root = tk.Tk() # We then create the root
optionList = ('a', 'b', 'c') # And a list for the selections
v = tk.StringVar() # Then create a stringvar
v.set(optionList[0])  # Here is the initially selected value (we set the stringvar to optionlist[0], aka choose the "a")
om = tk.OptionMenu(root, v, *optionList) # Create the optionmenu
om.pack() # Pack it

v.set(optionList[2]) # This one will be the final selected value("c"), you can change the number to 1 or 0 to fit your needs.
root.mainloop() # And mainloop

"""
FYI, the numbering starts from 0, so to select "a" you need to change the varaible to "0"
"""