[tkinter] Event from menu event?

kiki kiki at darkstar.frop.org
Mon May 24 21:36:10 EDT 1999


Newbie here.

I'm writing a simple drawing program using Tk.  I would like to treat
the drawn elements as a list of individual instantiations of drawing
classes -- line, blob, fractal, etc.  I would like to be able to pick a
menu item to create the object and then begin shaping it on the canvas.

As in: Click on "blob", a new blob gets instantiated and appended to the
object list, and I draw blob.  Now I click on "line" and draw some
lines.

So far so good.

I also want to allow other users to write arbitrary modules of drawing
primitives and append them at run time.  [That actually works fine.]
But it means the list of drawing classes at run-time is arbitrary, so no
hard-coding of all menu calls.

Here's the tricky part.

Since none of the drawing objects have been instantiated [they are
instantiated at menu-event time] I need to bind a function to each menu
item to do the instantiation.  As far as I can tell, no event data is
handed back to the bound function -- I have no way of knowing within my
called function *which* menu item was chosen.

It sure would be handy to be able to control multiple menu items with
one
function, but how would it tell the events apart??

This would be cool:

def mondo_menu_function(event):
    global drawing_object_list

    object_selected = eval(event.widget.text)
    new_object = object_selected(<init params>)
    drawing_object_list.append(new_object)

[Probably a bit more complicated than this, but you get the idea.]

I know you can use "bind" with buttons to bind a function to an event,
and that the function gets an event passed to it.  But menus look to be
different.  I can mymenu.add_command(label = "foo", command = myfunc)
but if I def myfunc this way:

def myfunc(event):
    print event

I get:

TypeError: not enough arguments; expected 1, got 0

I was thinking there might be a similar "bind" type of function for
menus, like button, but what would I bind it *to*?  I'm just adding a
menu item, not creating a button.

I know there's just *got* to be a way to write a generalized
menu-item-handling function.  Certainly *someone* has wanted a menu of,
say, colors, or bookmark items, or whatever, where the action is the
same for a group of menu items.

[And remember, I can't make a class function because the function
*instantiates* the class at menu-event time.  Yeah, sure, I'd love to
add the menu_call function to the superclass, but that's useless in this
case.]

Any ideas?

TIA!

Kiki -- I await your great collective wisdom... :-)





More information about the Python-list mailing list