[Tutor] Help with class in class

Dave Angel d at davea.name
Sun Sep 9 23:13:37 CEST 2012

On 09/09/2012 04:56 PM, leam hall wrote:
>     self.blue = Button(root, text="Blue",
> command=self.change_text_color("blue"))
>     self.blue.pack(side=LEFT)
>     self.green = Button(root, text="Green",
> command=self.change_text_color("green"))
>     self.green.pack(side=LEFT)
> To follow up, I've added a second button. Of course, it doesn't work, the
> text comes out as the color of the last button. It is running the commands
> in the Button line without the buttons being clicked.
> More research...

Please reread Peter's message, starting with "Another problem..."

He identifies the problem, and mentions one way to fix it.  In your
response, you mentioned to lambda, so you must have seen it.

In particular, the Button() function takes a function object as its
command= parameter.  You call the function yourself, and pass the return
value, which of course is not what's wanted.  You don't want the
function called till someone presses the button.  So you want something
equivalent to
      = Button(root, text="Blue", command=change_to_green)

Notice that there are NO parentheses on that change_to_green.  You want
the function object, you do NOT want to call the function.



More information about the Tutor mailing list