[Tkinter-discuss] How to set a constant in lambda

Guilherme Polo ggpolo at gmail.com
Tue Jan 27 18:15:54 CET 2009


On Tue, Jan 27, 2009 at 2:51 PM, vtcodger <donaldkenney at gmail.com> wrote:
>
> I came across a problem that probably has a simple, clever, answer -- but it
> is eluding me.  I was trying to create a set of tab buttons on a Tkinter
> page using Buttons.  These are created dynamically when the user loads a
> file.  I don't know in advance how many files will get opened, so I want to
> have a variable number of tabs.
>

You could consider using a Notebook widget.

> So we have a Frame T; and an array of tabs XT[]; and a function
> switch_tabs(n) that will handle the details of switching the data base,
> titles, etc.  I compute the tab number:
>
> Whoopsie = len(XT)
>
> And I create a new Button:
>
> XT.append(Button(T,text="Viola:New Tab",command=lambda:
> switch_tabs(whoopsie))
>
> It all works fine except for this one minor detail.  Whoopsie can't be a
> constant because I don't know in advance what constant to use.

This makes my head hurts. Email's title was: "How to set a constant in
lambda", but you just said you don't want it.

> And if
> whoopsie is a variable, it apparently is evaluated at execution time, and
> will have the then current value of whoopsie, not the value I wanted to set
> at creation time.

Constants and variables do not exist at Python, they are all names.
Can I assume that by "constant" you mean a immutable type and by
"variable" a mutable type ?

You could have put better names for these things instead of "whoopsie,
Whoopsie" to help a bit. So, what is switch_tabs supposed to do ? And
how this 'whoopsie' thing affects it ? Does it move you to the latest
tab created ? If this is the case, you could consider a pre-defined
value like 'end' or even None maybe so switch_tabs can check for these
values and understand they mean: "move to the latest tab".

> I tried everything I could think of including
> copy.copy(whoopsie) to get a constant set so that switch_tabs could know
> which tab to switch to.
>
> Couldn't do it.  I was starting to look at tracing back through stack frames
> when it came to me that RadioButtons unlike generic Buttons have a
> variable-value attribute pair that behaves as I desire.

What do you desire ?

> So this
> manifestation of the problem is worked around.  But I expect that I'll see
> this problem again in some other guise.


-- 
-- Guilherme H. Polo Goncalves


More information about the Tkinter-discuss mailing list