Newbie (faq!): pmw/tkinter menubar command-option

Niels Diepeveen niels at endea.demon.nl
Fri Aug 4 14:04:49 EDT 2000


Brian Elmegaard schreef:
> 
> A menu is created with an entry like:
>         menuBar.addmenuitem('ComponentList','command',
>                             label='Round Thing',
>                             command=self.addComponent(150,30))
> 
> where addComponent says:
>     def addComponent(self,x,y):
>         width = 10
>         height = 10
>         self.canvas.create_oval(
>             x - width, y - height, x + width, y + height)

Whatever you put in command=... has to be a function (or other callable
object) without arguments. The simplest way to do that is to define a
handler method for each item, like
    def __init__(self, ....):
...
...
        menuBar.addmenuitem('ComponentList','command',
                            label='Round Thing',
                            command=self.on_cmd_round_thing)
...
...
    def on_cmd_round_thing(self):
        self.add_component(150, 30)

An other option would be:
        menuBar.addmenuitem('ComponentList','command',
                     label='Round Thing',
                     command=lambda self=self:
self.addComponent(150,30))
but I can't really recommend that.

-- 
Niels Diepeveen
Endea automatisering




More information about the Python-list mailing list