I also dislike lambdas for this purpose. I'd recommend that the original
poster build a class - many ways they could go with this, but here is one
rough sketch (haven't checked for syntax errors, etc.):
class PageButton(Button):
def __init__(self, master, text=None, page_number=0):
self.page_number = page_number
self.text = text
if not self.text:
self.text = 'Page #' + str(self.page_number)
Button.__init__(self, master, text=self.text,
command=self.switch_tabs)
self.master = master
def switch_tabs(self):
# do whatever is required to switch tabs here, using self
to reference the button and self.page_number to reference the page
XT=[]
# command to create a new button
XT.append(PageButton(T, page_number=len(XT)))
Dave
David J. Giesen | Research Scientist | FPEG US Display OLED Materials R+D
|
Eastman Kodak Company | 2/83/KRL MC02216 | Rochester, NY 14650 |
[email protected] | 1-585-588-0480 Office |
www.kodak.com
Guilherme Polo <[email protected]> wrote on 01/29/2009 12:00:40 PM:
> On Thu, Jan 29, 2009 at 2:15 PM, <[email protected]> wrote:
> > With lambda, you need to set the value of the variable at the time the
> > lambda is created, or else the variable is grabbed from the
environment at
> > the time the lambda runs. You can do this by using the variable as a
> > default argument. In your case, change:
> >
> > XT.append(Button(T,text="Viola:New
> > Tab",command=lambda:switch_tabs(whoopsie))
> >
> > to
> >
> > XT.append(Button(T,text="Viola:New Tab",command=lambda
> > x=Whoopsie:switch_tabs(x))
> >
>
> Ah, it looks like you got what he meant.
>
> And to me, when you start having problem with lambda and solves it by
> doing lambda bindings, that is a good time to move to real functions.
>
> > ought to do it.
> >
> > Dave
> >
> > David J. Giesen | Research Scientist | FPEG US Display OLED Materials
R+D
> > |
> > Eastman Kodak Company | 2/83/KRL MC02216 | Rochester, NY 14650 |
> > [email protected] | 1-585-588-0480 Office |
> > www.kodak.com
> >
> >
>
> --
> -- Guilherme H. Polo Goncalves
> 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.
>
_______________________________________________
Tkinter-discuss mailing list
[email protected]
http://mail.python.org/mailman/listinfo/tkinter-discuss