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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…