Skip to content Skip to sidebar Skip to footer

More On Tkinter Optionmenu First Option Vanishes

This is a followup to my question here. I'm trying to use ttk.OptionMenu to enhance the look and feel of a dropdown menu. But as noted in the post here, 'A ttk optionmenu widget st

Solution 1:

That post you quote is incorrect. You do not need to put a space as the first item in the list. The signature of the ttk.OptionMenu command requires a default value as the first argument after the variable name.

You do not need to add an empty item to your dictionary. You do need to add a default value when you create the optionmenu:

self.optionmenu_a = ttk.OptionMenu(self.frame1, self.variable_a, "Asia", *self.dict.keys())

A slightly better solution would be to get the list of keys and save them to a list. Sort the list, and then use the first element of the list as the default:

options = sorted(self.dict.keys())
self.optionmenu_a = ttk.OptionMenu(self.frame1, self.variable_a, options[0], *options)

The reason you get the error is because you have a trace on the variable which calls a function that references self.optionmenu_b, but you are initially setting that variable before you create self.optionmenu_b. The simple solution is to create the second menu first.

self.optionmenu_b = ttk.OptionMenu(...)
self.optionmenu_a = ttk.OptionMenu(...)

Post a Comment for "More On Tkinter Optionmenu First Option Vanishes"