[Python] Should a tkinter button always have a variable as a name?

Discussão em 'Python' iniciado por Stack, Maio 7, 2025.

  1. Stack

    Stack Membro Participativo

    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?

    Continue reading...

Compartilhe esta Página