Skip to content Skip to sidebar Skip to footer

Python Tkinter, Setting Up Button Callback Functions With A Loop

I'm writing a program that displays a grid of buttons, when a button is pressed I want it to print the location of the button in the grid ('row column') out to the console. Here is

Solution 1:

In the function created using lambda, i, j refers to the varaible in the init_board function, which are set to 4, 4 after the for loop ends.

You can workaround this using default argument.


Replace the following line:

cmd = lambda: self.button_callback(i,j)

with:

cmd = lambda i=i, j=j: self.button_callback(i,j)

Post a Comment for "Python Tkinter, Setting Up Button Callback Functions With A Loop"