wasm-demo/demo/ermis-f/python_m/cur/1741

129 lines
3.4 KiB
Plaintext

From: kiki at pixar.com (kiki)
Date: Tue, 25 May 1999 09:13:46 -0700
Subject: [tkinter] Event from menu event?
References: <3749FE8A.9DD61AB4@pixar.com> <374A8853.F71C7218@earth.ox.ac.uk>
Message-ID: <374ACC3A.223E32BD@pixar.com>
Content-Length: 3154
X-UID: 1741
So far, no luck...
This is proving to be very tricky!!
I got this suggestion:
----
def myfunc(label):
print label
mymenu.add_command(label = "foo", command = lambda: myfunc("foo"))
----
Which works, but does NOT work if I'm doing something like this:
for each_class in draw_class_name:
mymenu.add_command(label = each_class, command = lambda:
myfunc(each_class))
I get this error:
Exception in Tkinter callback
Traceback (innermost last):
File "/usr/local/lib/python1.5/lib-tk/Tkinter.py", line 726, in __call__
return apply(self.func, args)
File "Haiku.py", line 167, in <lambda>
lambda: mondo_menu_call(each_class))
NameError: each_class
hee-hee!! Tricky! [This also applies to trying to pass integer index
parameters, of course. By the time it gets around to the menu call, the
context of the variable name is lost...]
Remember, since I'm loading modules dynamically, I can't know how many I have,
nor what their names are. I have a *list* of *strings* [or eval(draw_name) or
whatever you like) and have to build my menu based on variables, not
constants...
Nick Belshaw wrote:>
> and identify which menu-selection was made inside that function
>
> def commonfunc():
> called_index = mymenu.menu.index('active')
> operation = function_list[called_index]
This is the error [unfortunately] I get with that:
Menu selected Exception in Tkinter callback
Traceback (innermost last):
File "/usr/local/lib/python1.5/lib-tk/Tkinter.py", line 726, in __call__
return apply(self.func, args)
File "Haiku.py", line 134, in mondo_menu_call
print "Menu selected",pattern_data.haiku_menu.index('active')
AttributeError: index
Arg! Arg! <expletive deleted>
---Here is how I'm creating my menu [note, the names don't make any sense,
bare with me]:
class Haiku_Menu:
def __init__(self, master):
self.menubar = master
# Add the Haiku menu to the menubar [items will be added later in
Haiku.main]
self.haikumenu = Menu(self.menubar)
mbutton = Menubutton (self.menubar, text="Haiku", menu=self.haikumenu)
mbutton.pack(side=LEFT)
self.haikumenu = Menu(mbutton)
mbutton ['menu'] = self.haikumenu
def add_haiku_item(self, item_name, cmnd):
self.haikumenu.add_command(label = item_name, command = cmnd)
---main()
def main():
root = Tk()
pattern_data.hoop = Canvas(root, width = '4i', height = '4i')
# Make a bar separated from the drawing area
menubar = Frame (root, relief=RAISED, bd=2)
menubar.pack (side=TOP, fill=X)
# Add the relevent menu items
pattern_data.haiku_menu = Haiku_Menu.Haiku_Menu(menubar)
for stitch_name in stitch_class_list:
# Add the menu item for this stitch class and bind
# to the created function
pattern_data.haiku_menu.add_haiku_item(
eval(stitch_name).__name__,
mondo_menu_call)
pattern_data.hoop.pack(side = RIGHT)
root.mainloop()
---And here's my menu function:
def mondo_menu_call(): #\n"%eval(each_stitch_class).__name__,
global pattern_data
print "Menu selected",pattern_data.haiku_menu.index('active')
Thanx for your suggestions. They all work, just not with an unknown list of
strings.
Anybody else?
Kiki