473,231 Members | 1,783 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

Pmw optionMenu dynamic contents display?

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!

Jul 18 '05 #1
1 2079
Here's a test app to demonstrate the concept, and the difficulty.

#file testselectstate.py
title = 'LeakWarn System Selection'

# Import Pmw from this directory tree.
import sys
sys.path[:0] = ['../../..']

import Tkinter
import Pmw, re

global continentList, countryList, stateList

continentList = ['N.America','C. America', 'S. America']
countryList = [['Canada','USA','Mexico'],
['Guatemala','Nicaragua','Panama'],
['Venezuela','Colombia','Ecuador']]
stateList = [[['BC','Alberta','Saskatchewan','others'],
['California','Oregon','Washington','others'],
['Michoacan','Oaxaca','Monterrey','others']],
[['Guatemala states'],['Nicaragua states'],['Panama states']],
[['Venezuela states'],['Colombia states'],['Ecuador states']]]

# default selection
continentItem = continentList[0]
countryItem = countryList[0][0]
stateItem = stateList[0][0][0]

class selectSystem:
def __init__(self, parent):
# Create and pack the OptionMenu megawidgets.
# The first one has a textvariable.
self.var1 = Tkinter.StringVar()
self.var2 = Tkinter.StringVar()
self.var3 = Tkinter.StringVar()
self.var1.set(continentItem) # N. America
self.var2.set(countryItem) # Canada
self.var3.set(stateItem) # B.C.

self.method1_menu = Pmw.OptionMenu(parent,
labelpos = 'w',
label_text = 'Select Continent:',
menubutton_textvariable = self.var1,
items = continentList,
menubutton_width = 20,
menubutton_direction = 'flush',
command = self._getSelection
)
self.method1_menu.pack(anchor = 'w', padx = 10, pady = 10)

self.method2_menu = Pmw.OptionMenu (parent,
labelpos = 'w',
label_text = 'Select country:',
menubutton_textvariable = self.var2,
items = countryList[0],
menubutton_width = 20,
menubutton_direction = 'flush',
command = self._getSelection
)
self.method2_menu.pack(anchor = 'w', padx = 10, pady = 10)

self.method3_menu = Pmw.OptionMenu (parent,
labelpos = 'w',
label_text = 'Select state:',
menubutton_textvariable = self.var3,
items = stateList[0][0],
menubutton_width = 20,
menubutton_direction = 'flush' ,
command = self._getSelection
)
self.method3_menu.pack(anchor = 'w', padx = 10, pady = 10)

menus = (self.method1_menu, self.method2_menu, self.method3_menu)
Pmw.alignlabels(menus)

# Create the dialog.
self.dialog = Pmw.Dialog(parent,
buttons = ('OK', 'Apply', 'Cancel', 'Help'),
defaultbutton = 'OK',
title = 'Select State',
command = self.execute)
self.dialog.withdraw()

# Add some contents to the dialog.
w = Tkinter.Label(self.dialog.interior(),
text = 'Pmw Dialog\n(put your widgets here)',
background = 'black',
foreground = 'white',
pady = 20)
w.pack(expand = 1, fill = 'both', padx = 4, pady = 4)

def showAppModal(self):
self.dialog.activate(geometry = 'centerscreenalways')

def execute(self, result):
print 'You clicked on', result
if result not in ('Apply', 'Help'):
self.dialog.deactivate(result)

def _getSelection(self, choice):
# Can use 'self.var.get()' instead of 'getcurselection()'.
print 'You have chosen %s : %s : %s' % \
(self.var1.get(),
self.var2.get(),
self.var3.get() )
print choice # debug
i2 = indexContinent(self.var1.get())
self.var2.set(countryList[i2][0])
countryItem = countryList[i2]
#print pipelineItems # debug
self.method2_menu.config(items = countryList)
#s3 = systemElements.indexpipe(s2,test2)

def __call__(self):
self.dialog.show()

def indexContinent(name):
found = 'false'
for i in range(len(continentList)):
check = continentList[i]
# print 'checking %s in %s' % (name, check) # debug
if re.search(name,check):
found = 'true'
break
print found
if (found=='true'):
#print 'index of %s is %s' % (name,i) # debug
return i
else:
return -1

def indexCountry(continentindex, name):
found = 'false'
for i in range(len(countryList[continentindex])):
check = countryList[continentindex][i]
# print 'checking %s in %s' % (name, check) # debug
if re.search(name,check):
found = 'true'
break
print found
if (found=='true'):
#print 'index of %s is %s' % (name,i) # debug
return i
else:
return -1
################################################## ####################

# Create selectSystem in root window for testing.
if __name__ == '__main__':
root = Tkinter.Tk()
Pmw.initialise(root)
root.title(title)

OKButton = Tkinter.Button(root, text = 'OK', command = root.destroy)
OKButton.pack(side = 'bottom')

widget = selectSystem(root)
root.mainloop()

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stewart Midwinter | last post by:
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...
2
by: Jeffrey Barish | last post by:
Is there a way to fill the values in an OptionMenu dynamically? I need something like the add_command method from Menu. Is there a better way to implement a pull-down list? -- Jeffrey Barish
0
by: Martin | last post by:
I'd like to change the way the button that appears in an OptionMenu instance, but have been umable to figure out how to do this. The default constructor won't allow you to pass an 'image=" keyword....
0
by: mariox19 | last post by:
Hello, The Tkinter OptionMenu widget has me a bit confused. I have set up an OptionMenu to expand along the X axis as the window expands. What I find though is that the width of the submenu...
1
by: erikober | last post by:
I'm creating a OptionMenu button for a gui and I'm having a problem where the drop down list is so long that most of the options are off screen. The correct behavior would be that another drop...
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
7
by: bu | last post by:
I have a form with a handful of comments fields. I am trying to code the form in such a way that when the user clicks on the field, a dialog box will open up displaying the full contents of the...
0
by: | last post by:
Hi, I am just progamming using asp.net, and I want create a usercontrol to display some dynamic(from database) contents. Is this possible? Is there any sample code ? Thanks in advance! ...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.