Search code examples
pythontkintertkinter-button

How to make a button with a command with the parameter/attribute of its name with tkinter?


How to make a button with a command with the parameter/attribute of its name with tkinter?

from tkinter import *

def openfile(name):
    print(name)

for i in l:
    if num == 2:
        num = 0
        y = y+100
        x = 100
        print(y)
        print(x)
        print(num)

    bt = Button(second_frame, text=i, command=lambda: openfile(i)).grid(row=y, column=x, pady=10, padx=10)
    num = num + 1
    x = x + 100
    print(num)

I want to set the button command to be open file then its name as an attribute. I don't know how to get the buttons name on the same line.

NOTE: The button name differs from each button


Solution

  • You mean something like this ?

    for i in range(5):
       btn_name=str(i)
       btn_obj=Button(second_frame,text=btn_name,command=lambda name=btn_name:openfile(name))