I can make a tkinter button like this:
button1 = ttk.Button(root, text = "Button 1", command=lambda: button_click(button1))
or in a loop like this:
tk.Button(root, text=f"Button {i}", command=lambda x=i: button_click(x)).pack()
But the second button does not have a variable name like button1. Therefore, I can't access it by name:
def button_click(button):
button_text = button.cget('text')
ttk.Label(root, text = button_text).pack()
Or does the second button actually have a tkinter default name when created in a loop like this?
You can use the first one even in a for loop:
def button_click(button):
ttk.Label(root, text=button["text"]).pack()
for i in range(1, 5):
btn = tk.Button(root, text=f"Button {i}")
btn.pack()
btn.config(command=lambda btn=btn: button_click(btn))