Pmw optionMenu dynamic contents display?

stewart stewart at midtoad.homelinux.org
Mon Feb 23 01:54:10 EST 2004


I'm writing an app that requires a 3-level optionMenu display.  Basically
I'm showing the contents of a 3-dimensional matrix.  You choose the first
level in the first optionMenu, the 2nd level in the next level, and the 3rd
level in the last optionMenu.  Let's call them Continent,country,state.

continentList = ['N.America', 'S. America', 'Europe', 'Asia', Africa',
'Australia', 'Antarctica']
countryList = [['Canada', 'USA', 'Mexico'],['Argentina','Chile', ..],[other
continents' countries... ]]
stateList = [[['BC','Alberta', ...
'Newfoundland'],['California','Oregon',...'Maine'],['Guadalajara',...]],[[other
continents' countries' states]]

When I open the window, all is well. I can display the following as default
selections:
[ N. America ]
[  Canada    ]
[   B.C.     ]

Making a selection of a continent, country or state invokes a _getSelection
method that displays the 3 choices. So far so good.

Also, if I change the continent selection, then I get a different default
country in the 2nd optionMenu.  But, this is where is goes wrong.  I can't
get the list of countries to be updated in the 2nd optionMenu, nor the list
of states to be updated in the state list.  How would I do this?

I've tried to include some code at the end of the _getSelection method but
it isn't working. Does anyone has any similar code that they can share?

global continentItems, countryItems, stateItems

def _getSelection(self, choice):
        print 'you have chosen %s %s %s' % \
        (self.var1.get(),
         self.var2.get(),
         self.var3.get() )
        # above code correctly prints the selected choices from all 3 levels
        # next part of code tries to re-draw country list based on 
        # continent selection, but it doesn't work.
        # next line successfully gets list index of chosen continent
        i2 = indexContinents(self.var1.get()
        # first item in countryList is name of associated continent
        self.var2.set(countryList[i2][0])
        # next line correctly contains list of countries for selected continent
        countryItems = countryList[i2]
        # next line croaks: trying to change the 'items' option for optionMenu
        # by directly addressing its 'items' parameter: no good
        self.method2_menu.config(items = countryItems)

stuck here!




More information about the Python-list mailing list