How to get the name of a button tkinter python How to get the name of a button tkinter python tkinter tkinter

How to get the name of a button tkinter python


You can pass the number to the callback function with lambda:

import tkinter as tkdef f(name):    print ("Frame:", frm.winfo_children())    print(name)root = tk.Tk()frm = tk.Frame(root)for i in range (0, 3):    bt = tk.Button(frm, text = str(i), name = str(i), command = lambda i=i: f(str(i)))    bt.pack()frm.pack()root.mainloop()


This should work

import tkinter as tkdef f ():     print ("Frame:", frm.winfo_children())root = tk.Tk() frm = tk.Frame(root)for i in range (0, 3):     bt = tk.Button(self, frm, text = str(i), name = str(i), command = f)    bt.pack() frm.pack()root.mainloop()