[tkinter] Event from menu event?

Nick Belshaw nickb at earth.ox.ac.uk
Tue May 25 07:24:04 EDT 1999


kiki wrote:

> Newbie here.
>
> I'm writing a simple drawing program using Tk.
>

- should be fine. Tk can handle all this. You can add to ( and delete from
) your created menu any time you like. Along the lines you mention

> 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
>

What you need there is simply....

def myfunc():
    pass # do what you like

since, as you note no parameters are passed ( can be done with lambda but I
haven't !)

- which will then operate without raising the exception.

Alternatively you can bind the same command to every menu entry you add

mymenu.menu.add('command', label='myfunc', command=commonfunc)

and identify which menu-selection was made inside that function

def commonfunc():
    called_index = mymenu.menu.index('active')
    operation = function_list[called_index]

You could then use the index to call instances held in an array matching
the menu list which you extend/modify as you extend/modify the menu.

Alternatively.....

Tooo many choices, but hope that does not confuse too much :-/

cheers
nick/oxford





More information about the Python-list mailing list