Home > OS >  Tkinter Display Label does not udpate
Tkinter Display Label does not udpate

Time:01-10

The displayed score on the Tkinter window does not update on each loop. But if you check in the terminal score is been updated

Tk Window does not update enter image description here

The terminal score has been updated enter image description here

score = 0

def check():
    global score
    if answered_question[1]['correct_answer'] == 'True':
        score  = 1

root = tk.Tk()
root.config(padx=20, pady=20, bg=BLUE_GRAY)

score_board = tk.Label(
    text=f"Score: {score}",
    bg=BLUE_GRAY, fg="white",
    font=('Arial', 10, "normal")
)

check_btn = tk.Button(image=check_img, command=check, highlightthickness=0)

root.mainloop()

CodePudding user response:

You have to update the label explicitly by using either config/configure (same thing), or by binding a tk.StringVar() to your Label's textvariable parameter

# Option One
def check():
    global score
    if answered_question[1]['correct_answer'] == 'True':
        score  = 1
        # update the label
        score_board.configure(text=f'Score: {score}')

or...

# Option Two
def check():
    global score
    if answered_question[1]['correct_answer'] == 'True':
        score  = 1
        # update the label
        label_var.set(f'Score: {score}')

...  # code omitted for brevity

label_var = tk.StringVar()  # declare a tk.StringVar to bind to your label

score_board = tk.Label(
    text=f"Score: {score}",
    bg=BLUE_GRAY, fg="white",
    font=('Arial', 10, "normal"),
    textvariable=label_var,  # the label will update whenever this var is 'set'
)
  • Related