[Python] How can I make a button that adds one to a variable after each press? [closed]

Discussão em 'Python' iniciado por Stack, Janeiro 23, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to make a button that adds one to a variable each time it is clicked so that the variable goes up like 1, 2, 3, etc. this is what I have so far:

    def main():
    root = tk.Tk()
    root.geometry('200x150')
    # /////////////////
    global count
    count = 0

    def txtChanger():
    global count
    count =+ 1
    label.config(text = count)
    print(count)

    label = ttk.Label(root, text = '')
    label.pack()

    button = ttk.Button(root, text = 'click to change label', command=txtChanger)
    button.pack()
    # ///////////////////
    root.mainloop()


    when I press my button, it does change the label to 1 but it does not actually add up, the print in the txtChanger brings back 1 over and over again, what am I doing wrong?

    I've tried this without 'count' being a global variable and just have the txtChanger function return 'count' and that didn't work

    Continue reading...

Compartilhe esta Página