[Tutor] Help with class in class
leam hall
leamhall at gmail.com
Sun Sep 9 21:12:48 CEST 2012
On Sun, Sep 9, 2012 at 12:12 PM, Peter Otten <__peter__ at web.de> wrote:
> the above will no longer complain about a missing attribute.
>
> > root = Tk()
> > project = ch8_Project(master=root)
> > project.mainloop()
>
>
> Another problem that caught my attention:
>
> > self.green = Button(root, text="Green",
> command=self.change_text_color("green"))
>
>
> The command argument is supposed to be a function; you are instead
> assigning
> the result of a method call (which is None in this case, but as a side
> effect will set the color to green immediately. The simplest fix is to
> define a helper function that takes no arguments
> ...
> def change_to_green():
> self.change_text_color("green")
> self.green = Button(root, text="Green", command=change_to_green)
> ...
>
> If you already know about lambda you can try to rewrite this using that.
>
>
Peter, thank you for helping me understand the scope issue I was missing! I
knew it was something like that but forget that omitting "this." meant the
variables were limited in scope to that method.
I have seen lamba but do not understand it. The project requires four
buttons, each one turns the same text a different color. The class is also
Python 3 based. :)
Leam
--
Mind on a Mission <http://leamhall.blogspot.com/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120909/94464518/attachment.html>
More information about the Tutor
mailing list