[Python] TextVariable not updating in Tkinter

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

  1. Stack

    Stack Membro Participativo

    I'm using Tkinter for the first time and can't seem to update the text on a label. I've tried using text variable and it still doesn't work. I've done some without the function but it still doesn't update. Here's my full code:

    from tkinter import *
    from tkinter import ttk
    root = Tk()
    frm = ttk.Frame(root, padding=10)
    frm.grid()
    global money
    money = IntVar()
    money.set(0)
    global num
    num = 0

    def button_clicked():
    num += 1
    money.set(num)

    ttk.Label(frm, text="Hello World!").grid(column=0, row=0)
    ttk.Label(frm, text = money.get()).grid(column=0, row=1)
    ttk.Button(frm, text="Quit", command=root.destroy).grid(column=1, row=0)
    ttk.Button(frm, text="Add Money", command=lambda: button_clicked).grid(column=1, row=1)
    root.mainloop()


    Tried updating the variable in the command section of the button, didn't change anything, still displayed zero. Converted it from IntVar to StrVar and didn't work, Added the Num variable and didn't change anything either.

    Continue reading...

Compartilhe esta Página