Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python tkinter button command dont runing right parameter

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:
enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading