简体   繁体   中英

How to change the text of a Label - Tkinter

I am creating a math game and want a score count in the top corner.

Yes, you need the command argument, but you also need a function for the command argument to run. Something like this would work:

from tkinter import *

def increment_score():
    global score
    score += 1 # update variable
    score_addition_easy_number.config(text=score) # update screen

root = Tk()

score = 0
root7 = Frame(root)
score_addition_easy_label = Label(root7, text="Score count: ")
score_addition_easy_label.pack(side=LEFT)
score_addition_easy_number = Label(root7, text=score)
score_addition_easy_number.pack(side=LEFT)
root7.pack()

a_e_answer_2 = Button(root, text="8", font=("times", 30, "bold"), padx=190, command=increment_score)
a_e_answer_2.pack()

root.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM