Tkinter Menu command

Raimo Tuisku raimo-news at mail.com
Sat Jul 13 11:58:59 EDT 2002


Is it possible to pass arguments to a method which is executed after
event? I am trying to do so with add_command method from Menu widget to
avoid writing separate method for every case.
#From class App:
    self.mb = Menubutton(self.frame, text="Color") 
    self.mb.grid()
    self.mb.menu = Menu(self.mb,tearoff=0) 
    self.mb['menu'] = self.mb.menu
    self.colors=['red','green','blue']
    for item in self.colors:
        self.mb.menu.add_command 
        (label=item,command=lambda :self.set_color(item))
    # Even this doesn't work. Apparently it always uses
    # self.set_color('blue'). (self.label colors to blue)
# Method for setting the color in class App:
def set_color(self,color):
    self.label.config(fg=color)

-- 
Raimo Tuisku



More information about the Python-list mailing list