[Python] How to return a variable from a Tkinter button command function?

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

  1. Stack

    Stack Membro Participativo

    I am trying to make a button that increments a variable with Tkinter, but when I 'call' (I know it isn't really calling) a function with command, i can not use return variable, as there is nowhere to return the variable to. Are there alternative ways to do this? Here is my code:

    import tkinter as tk

    variable = 0

    root = tk.Tk()


    def variable_incrementer():
    global variable
    variable += 1
    # Not return here


    click_btn = tk.Button(root, text="Click me", command=variable_incrementer)
    click_btn.pack()

    root.mainloop()

    Continue reading...

Compartilhe esta Página