Taylor's Blog

Atypical ramblings

Solving the OptionMenu Mishap

Once again, Steve has pulled of some programming wizardry. He has successfully discovered the solution to my OptionMenu problem. It turns out that I could not use the .set() method because my importation of ttk’s OptionMenu made it non-functional. Once ttk’s OptionMenu has been set as the OptionMenu I’m going to use, I need to set the initial value of the menu following ttk’s rules, not tkinter’s. In tkinter, an option menu follows this pattern:

myMenu = OptionMenu(parent, variable, choice1, choice2, ...)

but ttk’s follows this pattern:

myMenu = OptionMenu(parent, variable, defaultValue, choice1, choice2, ...)

As you can see, tkinter’s version doesn’t have the default value located in the statement. It needs a separate statement to set the variable. Mainly: variable.set(value). I was trying to mix the tkinter .set() method, with the ttk OptionMenu which didn’t jive well. So, without further ado, here is the correctly working code:
[python]
from tkinter import *
from ttk import OptionMenu

root = Tk()

def buttFunction():
myMenu.set_menu(‘New item 1’, ‘New item 2’)

a = StringVar(root)

myMenu = OptionMenu(root, a, ‘select’, ‘Item 1’, ‘Item 2’, ‘Item 3′)
myMenu.pack()

butt = Button(root, text=’push’, command=buttFunction)
butt.pack()

root.mainloop()
[/python]

Updated: April 23, 2015 — 5:27 pm

Leave a Reply

Your email address will not be published. Required fields are marked *

Taylor's Blog © 2015