Dec-01-2017, 06:52 PM
The event handler doesn't have any concept of which
You can get around that by having a local variable for the event handler which is unique to each event handler. As was shown above, one way to do that is with a class. Another is to build a new event handler each time, like so:
i
it should be referring to. It doesn't capture the value when you create it, and instead uses whatever i
is when the event handler fires off, which is 9 (unless you click it really fast, before all of them exist).You can get around that by having a local variable for the event handler which is unique to each event handler. As was shown above, one way to do that is with a class. Another is to build a new event handler each time, like so:
# this function is unchanged from your code... def click(num): print(num) def build_click_handler(num): return lambda: click(num) for i in range(10): b = tkinter.Button(root, text = i, command = build_click_handler(i))