<br><br><div class="gmail_quote">On Mon, Nov 2, 2009 at 2:26 AM, Peter Otten <span dir="ltr"><__<a href="mailto:peter__@web.de">peter__@web.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<div class="im">Alf P. Steinbach wrote:<br>
<br>
>>     for x in range(0,3):<br>
>>         Button(......, command=lambda x=x: function(x))<br>
><br>
> An alternative reusable alternative is to create a button-with-id class.<br>
><br>
> This is my very first Python class so I'm guessing that there are all<br>
> sorts of issues, in particular naming conventions.<br>
<br>
</div>Pseudo-private attributes, javaesque getter methods, unidiomatic None-<br>
checks, broken naming conventions (**args), spaces in funny places...<br>
<div class="im"><br>
> And the idea of creating a reusable solution for such a small issue may be<br>
> un-pythonic?<br>
<br>
</div>Screw pythonic, the signal/noise ratio is awful in any language.<br>
<div class="im"><br>
> But just as an example, in Python 3.x,<br>
<br>
</div>...for achieving less in more lines?<br>
<div><div></div><div class="h5"><br>
> <code><br>
> import tkinter<br>
> # I guess for Python 2.x do "import Tkinter as tkinter" but haven't<br>
> # tested.<br>
><br>
><br>
> class IdButton( tkinter.Button ):<br>
>      def __init__( self, owner_widget, id = None, command = None, **args<br>
>      ):<br>
>          tkinter.Button.__init__(<br>
>              self, owner_widget, args, command = self.__on_tk_command<br>
>              )<br>
>          self.__id = id<br>
>          self.__specified_command = command<br>
><br>
>      def __on_tk_command( self ):<br>
>          if self.__specified_command != None:<br>
>              self.__specified_command( self )<br>
>          else:<br>
>              self.on_clicked()<br>
><br>
>      def on_clicked( self ):<br>
>          pass<br>
>      def id( self ):<br>
>          return self.__id<br>
>      def id_string( self ):<br>
>          return str( <a href="http://self.id" target="_blank">self.id</a>() );<br>
><br>
><br>
> def on_button_click( aButton ):<br>
>      print( "Button " + aButton.id_string() + " clicked!" )<br>
><br>
> window = tkinter.Tk()<br>
><br>
> n_buttons = 3<br>
> for x in range( 1, n_buttons + 1 ):<br>
>      IdButton(<br>
>          window, id = x, text = "Button " + str( x ), command =<br>
>          on_button_click ).pack()<br>
><br>
> window.mainloop()<br>
> </code><br>
<br>
</div></div>I'm not grumpy, I just don't like your code ;) And I don't like the notion<br>
that you are about to spread this style with your book...<br>
<font color="#888888"><br>
Peter</font></blockquote><div><br></div><div>I was going to agree with you (</div><div>particularly about this</div><div>)</div><div>but then I saw your __email address__ and realized that you, too, have no style. </div>

</div>