In Tkinter how can I correct this loop/function so that each button changes a value to the value of the button?
This is a simplified version of my code, at the moment each button changes the value of size to 15 rather than the number on the button. I was wondering if there was anyway of fixing this loop without printing each individual button and value without a loop?
from Tkinter import *
size = 7
def AI():
AIBoard = Tk()
AIBoard.title("Board Select")
BoardSize = Label(AIBoard, text = "Please pick a board size: ", font = ('Helvetica',20))
BoardSize.pack(side = 'top')
for a in range(5,16,1):
sizeBut = Button(AIBoard, text = a, width = 5, command = lambda: inputBoardSize(a))
sizeBut.pack(side = 'left')
AIBoard.mainloop()
def inputBoardSize(x):
size = x
print size
AI()
Thanx
Change your lambda to bind the value at the time the anonymous function is created.