Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何更改标签的文本-Tkinter_Python_Tkinter_Button_Label - Fatal编程技术网

Python 如何更改标签的文本-Tkinter

Python 如何更改标签的文本-Tkinter,python,tkinter,button,label,Python,Tkinter,Button,Label,我正在创建一个数学游戏,希望在上角计算分数。是的,您需要命令参数,但还需要一个函数来运行命令参数。类似这样的方法会奏效: 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 roo

我正在创建一个数学游戏,希望在上角计算分数。

是的,您需要命令参数,但还需要一个函数来运行命令参数。类似这样的方法会奏效:

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\u addition\u easy\u number是一个未解析的引用-它说创建一个参数,有什么想法吗?谢谢x@Katie010203这不是一个python错误,这是某种林特警告,对此我无能为力。然而,我可以看出它在抱怨你的代码布局。我将我的答案编辑为a,这样你就可以看到正确的布局。好的,谢谢你的帮助xxheyy我在函数中有从分数=0开始的所有内容,这就是为什么他们要求参数,因为我必须通过这些内容吗?谢谢x@Katie010203嗯,是的,那会导致它。黑客解决方案是将全局变为非局部,并将增量函数也放在函数中。正确的解决方法是创建一个类。