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.

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.  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.  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.  So this
manifestation of the problem is worked around.  But I expect that I'll see
this problem again in some other guise.

Am I missing something?  Probably something simple?  What?
-- 
View this message in context: 
http://www.nabble.com/How-to-set-a-constant-in-lambda-tp21689312p21689312.html
Sent from the Python - tkinter-discuss mailing list archive at Nabble.com.

_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss

Reply via email to