Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
140 views
in Technique[技术] by (71.8m points)

python - Checkbuttons and buttons: using lambda

I am trying to make a number of checkboxes based on a list, however it looks as if I am screwing up the command call and variable aspect of the button.

My code is:

class Example(Frame):

def __init__(self, parent):
    Frame.__init__(self, parent)

    self.parent = parent
    self.initUI()

def initUI(self):

    self.courses = ["CSE 4444", "CSE 4343"]
    self.vars = []

    self.parent.title("Homework Helper")

    self.course_buttons()

    self.pack(fill=BOTH, expand=1)

def course_buttons(self):
    x = 0
    y = 0

    for i in range(0, len(self.courses)):
        self.vars.append(IntVar())
        cb = Checkbutton(self, text=self.courses[i],
                         variable=self.vars[i],
                         command=lambda: self.onClick(i))
        cb.select()
        cb.grid(column=x, row=y)
        y = y+1

def onClick(self, place):

    print place
    if self.vars[place].get() == 1:
        print self.courses[place]

The test so far is for the course to be printed on the console when the check box is on, however it only works for the second button, button "CSE 4343". When I interact with button "CSE 4444", nothing is printed.

Also, the "place" value is onClick is always 1, whether I am clicking button "CSE 4343" or button "CSE 4444".

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When you make a lambda function, its references only resolve into values when the function is called. So, if you create lambda functions in a loop with a mutating value (i in your code), each function gets the same i - the last one that was available.

However, when you define a function with a default parameter, that reference is resolved as soon as the function is defined. By adding such a parameter to your lambda functions, you can ensure that they get the value you intended them to.

lambda i=i: self.onClick(i)

This is referred to as lexical scoping or closure, if you'd like to do more research.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...