How to assign variables in lambda for tkinter buttons

Viewed 182

I am trying to get a tkinter quiz to work for a school project but can't seem to get buttons to work. By virtue of how my loops work I cant make a function for this.

ansBtn1 = tk.Button(self.master, text=question[3], command=(lambda: choice = question[3]))
ansBtn1.grid(row=1, column=0)
1 Answers

Define a normal function:

def function():
    global choise
    choise = question[3]

ansBtn1 = tk.Button(self.master, text=question[3], command=function)
ansBtn1.grid(row=1, column=0)

If you really really want to use lambda, you can use the walrus operator. For more info read this.

Related