i want to make a 3×3 button interface. I tryed make this on tkinter but its didnt working right.
there is my main.py code:
frame = tk.Frame(root, bg='white')
frame.place(relx=0.5, rely=0.5, anchor='center')
buttons = []
for row in range(3):
row_list = []
for col in range(3):
button = tk.Button(
master=frame,
text="",
font=('Calibri', 36, "bold"),
fg="black",
width=5,
height=2,
highlightbackground="lightblue",)
button.config(command=lambda: button_listener(row, col))
button.grid(column=col, row=row, padx=10, pady=10)
row_list.append(button)
buttons.append(row_list)
root.mainloop()
and there is my func.py code:
import tkinter as tk
def button_listener(row, col):
print(f'Button pressed at row:{row}, col:{col}')
when im runing this code its look like running with successfuly but buttons doesnt working right.
there is interface:
I’ll click first button at first row. its printing me "Button pressed at row:2, col:2". It doesn’t matter which button I click. its always printing "Button pressed at row:2, col:2".
>Solution :
Change this:
button.config(command=lambda: button_listener(row, col))
To this:
button.config(command=lambda row=row, col=col: button_listener(row, col))