Tkinter question

Eric Brunel eric_brunel at despammed.com
Tue Oct 24 09:20:20 CEST 2006


On Tue, 24 Oct 2006 07:48:51 +0200, Hendrik van Rooyen  
<mail at microcorp.co.za> wrote:
>  Sorin Schwimmer  wrote:
> Hi All,
>
> Is it possible to have a widget id while it is created?
> Something like this:
>
> Button(root, text='...', command= lambda v=<widget id>: fn(v)).grid()
>
> and then the function:
>
> def fn(v):
>   v['bg']='BLUE' # or maybe nametowidget(v)['bg']='BLUE'

This cannot be done this way: when you create your lambda passed to the  
'command' option, the widget itself is not yet created. So there's no way  
at all to get the 'id' it will have when created.

Alternatively, you can simply split the widget creation in several lines:

b = Button(root, text='...')
b.grid()
b.configure(command=lambda b=b: fn(b))


But Hendrik's solution is better, since it avoids the use of lambda, which  
is often difficult to understand.

HTH
-- 
python -c "print ''.join([chr(154 - ord(c)) for c in  
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"



More information about the Python-list mailing list