How to link 3 Tkinter OptionMenu lists?

Peter Otten __peter__ at web.de
Wed Feb 25 04:04:28 EST 2004


Stewart Midwinter wrote:

> I would like to link the contents of three OptionMenu lists. When I select
> an item from the first list (call it continents), the contents of the 2nd
> list (call it countries) would update. And in turn the contents of the 3rd
> list (call it states would be updated by a change in the 2nd list.  If 

The items option is only available for OptionMenu.__init__(). To change the
items later, use setitems() instead. I suggest that you use separate
command methods for all three OptionMenu widgets. With what I think are
minimal changes:

def _getContinentSelection(self, choice):
    index = continentList.index(self.var1.get())
    self.method2_menu.setitems(countryList[index], 0)
    self._getCountrySelection()

def _getCountrySelection(self, choice=None):
    continentIndex = continentList.index(self.var1.get())
    countries = countryList[continentIndex]
    index = countries.index(self.var2.get())
    self.method3_menu.setitems(stateList[continentIndex][index], 0)

def _getStateSelection(self, choice):
    pass # your code

I think the lookup mechanism could be improved by using nested dictionaries.

Peter




More information about the Python-list mailing list