【问题标题】:How to change the text of a Label - Tkinter如何更改标签的文本 - Tkinter
【发布时间】:2020-12-28 19:23:28
【问题描述】:

我正在创建一个数学游戏,并希望在右上角显示分数。

【问题讨论】:

    标签: python tkinter button label


    【解决方案1】:

    是的,您需要命令参数,但您还需要一个函数来运行命令参数。这样的事情会起作用:

    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()
    

    【讨论】:

    • 嘿,感谢您的帮助,但它说 score_addition_easy_number(在新函数中)是一个未解决的参考 - 它说创建一个参数,有什么想法吗?谢谢x
    • @Katie010203 这不是 python 错误,而是某种 linter 警告,我对此无能为力。但是我可以说它在抱怨您的代码布局。我将答案编辑为minimal reproducible example,以便您可以看到正确的布局。
    • 嘿,我在一个函数中包含了 score=0 的所有内容,这就是他们要求参数的原因,因为我必须传递东西吗?谢谢x
    • @Katie010203 嗯,是的,这会导致它。 hack 解决方案是将global 更改为nonlocal,并将增量函数也放入您的函数中。正确的解决方案是创建一个类。
    • 上课会做什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多