2008-07-15: Selected articles (including this one) now have experimental "comment" links in the left column and at the bottom. You're welcome to use them for commenting and voting on articles. For a bit more on this, see this page. /F

back next

The Tkinter OptionMenu Widget

The OptionMenu class is a helper class that creates a popup menu, and a button to display it. The option menu is similar to the combobox widgets commonly used on Windows.

To get the currently selected value from an option menu, you have to pass in a Tkinter variable. See the patterns section for some examples.

Patterns #

To create an option menu, call the OptionMenu class constructor, and pass in the variable and a list of options.

from Tkinter import *

master = Tk()

variable = StringVar(master)
variable.set("one") # default value

w = OptionMenu(master, variable, "one", "two", "three")
w.pack()

mainloop()

To get the selected option, use get on the variable:

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

The following example shows how to create an option menu from a list of options:

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()
 
[comment on/vote for this article]

back next

Comment:

I like functional programming as much as the next guy, but unless you demand a high degree of backward compatiblity, I see no need for the somewhat obscure w = apply(OptionMenu, (master, variable) + tuple(OPTIONS)) when w = OptionMenu(master, variable, *OPTIONS) # note the '*' will do.

Posted by Bob Lewis (bobl@tricity.wsu.edu) (2007-03-06)

The * syntax didn't exist when this page was written. /F

A Django site. this page was rendered by a django application in 0.03s 2008-07-24 06:56:16.201424. hosted by webfaction.