Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkbuttons and buttons: using lambda [duplicate]

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".

like image 494
user2455869 Avatar asked Dec 01 '25 06:12

user2455869


1 Answers

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.

like image 94
TigerhawkT3 Avatar answered Dec 06 '25 10:12

TigerhawkT3



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!