OptionMenu类是一个辅助类,它用来创建弹出菜单,并且有一恶搞按钮显示它。它非常类似Windows上的下拉列表插件。
如果要获取当前选项菜单的值,你需要把它和一个Tkinter变量联系起来。
from Tkinter import *
master = Tk()
variable = StringVar(master)
variable.set("one") # default value
w = OptionMenu(master, variable, "one", "two", "three")
w.pack()
mainloop()
效果如下图:
from Tkinter import *
master = Tk()
var = StringVar(master)
var.set("one") # initial value
option = OptionMenu(master, var, "one", "two", "three", "four")
option.pack()
#
# test stuff
def ok():
print "value is", var.get()
master.quit()
button = Button(master, text="OK", command=ok)
button.pack()
mainloop()
运行效果如下:
from Tkinter import *
# the constructor syntax is:
# OptionMenu(master, variable, *values)
OPTIONS = [
"egg",
"bunny",
"chicken"
]
master = Tk()
variable = StringVar(master)
variable.set(OPTIONS[0]) # default value
w = apply(OptionMenu, (master, variable) + tuple(OPTIONS))
w.pack()
mainloop()
运行如下图: