472,779 Members | 2,842 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How to link 3 Tkinter OptionMenu lists?

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 anyone can share a
recipe or some ideas, I'd be grateful!

Here's some sample code that displays three OptionMenus, but doesn't update the
list contents :-(

---
#file selectSystem.py
title = 'linked OptionMenus'

# 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()
-----

--
Stewart Midwinter
running on Mandrake Linux 9.2
PGP public key at: http://www.keyserver.net
e-mail: Stewart 'at' Midwinter.ca, stewart 'at' midtoad.homelinux.org
web: http://www.midwinter.ca, http://midtoad.homelinux.org
voice: +1.403.714.4329

Umwelt schuetzen, Rad benuetzen!

Jul 18 '05 #1
2 4550
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

Jul 18 '05 #2
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 anyone can share a
recipe or some ideas, I'd be grateful!
Seeing your code below, you already know everything needed to do that. Read on...
Here's some sample code that displays three OptionMenus, but doesn't update the
list contents :-(

---
#file selectSystem.py
title = 'linked OptionMenus'

# 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']]]
I'd turn countryList and stateList into dictionaries like follows:

countryList = {
'N.America': ['Canada','USA','Mexico'],
'C. America': ['Guatemala','Nicaragua','Panama'],
'S. America': ['Venezuela','Colombia','Ecuador']
}
stateList = {
'Canada': ['BC','Alberta','Saskatchewan','others'],
'USA': ['California','Oregon','Washington','others'],
...
}

This is not really required, but it would make the selection of the items in the
sub-lists far easier. See below.
# default selection
continentItem = continentList[0]
countryItem = countryList[0][0]
stateItem = stateList[0][0][0]
With dictionaries, that would become:

countryItem = countryList[continentItem][0]
stateItem = stateList[countryItem][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
)
You should put in the command option here a method that will actually update the
next OptionMenu from the value selected by the user. You can use the method
setitems on it to change the list of items for the menu. Here is an example,
using dictionaries for countryList and stateList:

def _selectContinent(self, choice):
## Set appropriate list of countries
countries = countryList[self.var1.get())]
self.method2_menu.setitems(countries)
## If currently selected country is not in new list, select first valid one
if not self.var2.get() in countries:
self.var2.set(countries[0])
## Set appropriate list of states
states = stateList[self.var2.get()]
self.method3_menu.setitems(states)
## If currently selected state is not in list, select first valid one
if not self.var3.get() in states:
self.var3.set(states[0])
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
)
Same here: you should call via the command a method updating the list of states.
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)


The code for this method should not be called when a menu item changes, but when
the user validates the input. There is no need for the part starting at the call
of indexContinent.

[snip rest of code]

A few style remarks: you should name your attributes from what they represent
and not with numbered names like method1_menu or var3. If your class grows
bigger, in 6 months, I'm quite sure you won't remember that var2 is the country
(or was it the state?). Naming the attributes for the menus continentMenu,
countryMenu and stateMenu, and the corresponding variables continentVar,
countryVar and stateVar is a good habit to get: you'll make your programs far
more readable and maintainable.

HTH
--
- Eric Brunel <eric dot brunel at pragmadev dot com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com

Jul 18 '05 #3

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

Similar topics

1
by: stewart | last post by:
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...
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....
1
by: Martin | last post by:
Anybody know how to change the menu button that is displayed by the Tkinter Menubutton and the (derived from it) OptionMenu classes? I think it must be something from the Motif look and feel which...
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...
4
by: Alex Hunsley | last post by:
Can anyone recommend some code for creating drop-down menus in tkinter? To be absolutely clear, here's an example of a drop-down: http://www.google.co.uk/preferences?hl=en (see the language...
2
by: Chad | last post by:
Is there anyway to set the individual options in Tkinter to a particular variable. For example, I have a menu option(code is below) which has January, February, March and so on, which I would like...
44
by: bg_ie | last post by:
Hi, I'm in the process of writing some code and noticed a strange problem while doing so. I'm working with PythonWin 210 built for Python 2.5. I noticed the problem for the last py file...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.