The Tkinter OptionMenu widget and its usage

Saturday, May 23, 2009


The Tkinter OptionMenu is a GUI widget used to input multiple choice values, say selecting a date, month, year or age values, one can populate an OptionMenu intially with a tuple of option values and set a default value for the OptionMenu (Say a descriptive message "Select a Choice"), then once you select an option, you can get the selected option value through one of the OptionMenu properties or the OptionMenu control variable which is used to set the initial value.

We can create an OptionMenu widget through the python apply built-in method and initialize its keyword and non keyword arguments, in the below example we will see a simple OptionMenu with some option choices and when you click the Submit button, the selected option will be displayed when a valid choice is selected.

Listing 1: tkinterOptionMenu.py

1.  # File name: tkinterOptionMenu.py
2.  # Author: S.Prasanna
3.
4.  from Tkinter import *
5.  import tkMessageBox
6.
7.  def displayOption():
8.      """ Display the OptionMenu selection. """
9.
10.     global optionMenuWidget, DEFAULTVALUE_OPTION
11.
12.     if (optionMenuWidget.cget("text") == DEFAULTVALUE_OPTION):
13.         tkMessageBox.showerror("Tkinter OptionMenu Widget", "Select a valid option.")
14.     else:
15.         tkMessageBox.showinfo("Tkinter OptionMenu Widget", "OptionMenu value =" + optionMenuWidget.cget("text"))
16.
17. if __name__ == "__main__":
18.
19.     root = Tk()
20.     DEFAULTVALUE_OPTION = "Select an option."
21.
22.     root.title("Tkinter OptionMenu Widget")
23.     root["padx"] = 40
24.     root["pady"] = 20 
25.
26.     # Create an Option frame to hold the option Label and the optionMenu widget
27.     optionFrame = Frame(root)
28.
29.     #Create a Label in textFrame
30.     optionLabel = Label(optionFrame)
31.     optionLabel["text"] = DEFAULTVALUE_OPTION
32.     optionLabel.pack(side=LEFT)
33.
34.     # Create an optionMenu Widget in the optionFrame
35.     optionTuple = ("Option 1", "Option 2", "Option 3", "Option 4", "Option 5")
36.
37.     defaultOption = StringVar()
38.     optionMenuWidget = apply(OptionMenu, (optionFrame, defaultOption) + optionTuple)
39.     defaultOption.set(DEFAULTVALUE_OPTION)
40.     optionMenuWidget["width"] = 15
41.     optionMenuWidget.pack(side=LEFT)
42.
43.     optionFrame.pack()
44.
45.     button = Button(root, text="Submit", command=displayOption)
46.     button.pack()
47.
48.     root.mainloop()
Sample Output:

Figure 1: Tkinter OptionMenu

Explanation:

Though the above code uses an optionFrame (line 27), optionLabel (line 30) and an OptionMenu (line 38) which are wrapped inside the optionFrame, we will restrict our discussion only to the optionMenu.

The optionTuple (line 35) is a tuple with a different option choices, the defaultOption is a StringVar (line 37, a Tkinter String variable) which is used to set the initial value for OptionMenu and it will also hold the selected option choices. The apply function creates an OptionMenu widget with keyword and non-keyword parameters, its wrapped inside the optionFrame, associated with the defaultOption StringVar and initialized with the set of option values through the optionTuple.

The Submit button (line 45) invokes the displayOption (line 7) method when clicked, the displayOption menu fetches the OptionMenu selection choice through its method
optionMenuWidget.cget("text")
When this value is equal to DEFAULTVALUE_OPTION (line 20) where the user didn't select an option, a Tkinter error message is flashed, else the OptionMenu selection choice is displayed, in the next blog, we will see more sophisticated OptionMenu methods including adding/removing options on the fly and fetching a reference to the OptionMenu control variable.

3 comments:

Alex said...

Thanks for a very helpful and informative article. I've been having a lot of trouble finding a way to update an option menu dynamically and this worked perfectly.

Anonymous said...

Thanks, Keep it up !!

Anonymous said...

i have an error with defining Frame


Copyright © 2016 Prasanna Seshadri, www.prasannatech.net, All Rights Reserved.
No part of the content or this site may be reproduced without prior written permission of the author.