Python 问:为什么';标签';对象没有属性';设置';
这是我的数学游戏,但当Python 问:为什么';标签';对象没有属性';设置';,python,tkinter,tkinter-text,Python,Tkinter,Tkinter Text,这是我的数学游戏,但当'ask'str更新其错误时,问题不会改变 我试图通过改变代码位置来解决这个问题,但没有成功 检测到错误: 第25行,检索_输入 ..... 问题集合(询问) ..... AttributeError:“Label”对象没有属性“set” 所发生的事情并不是什么大不了的,只是尝试为每个项目指定不同的名称,因为python对标签的使用感到困惑。我尝试了代码,它成功地工作了 守则: from tkinter import * import random import time
'ask'
str更新其错误时,问题不会改变
我试图通过改变代码位置来解决这个问题,但没有成功
检测到错误:
第25行,检索_输入
..... 问题集合(询问)
..... AttributeError:“Label”对象没有属性“set”
所发生的事情并不是什么大不了的,只是尝试为每个项目指定不同的名称,因为python对标签的使用感到困惑。我尝试了代码,它成功地工作了 守则:
from tkinter import *
import random
import time
End = False
score=0
question=['13**2','5+1','60*2']
random.shuffle(question)
ask=question.pop()
text = ('text')
#command
def retrieve_input():
global ans,score,ask
if len(question)!=0:
ans=textBox.get("1.0","end-1c")
print(ans)
if int(ans) == int(eval(ask)):
score=int(score)+1
status_str.set('Score '+str(score))
random.shuffle(question)
ask=question.pop()
question_str.set(ask)
return score,ask
#app window
window = Tk()
window.title('Math Problem')
window.geometry('700x400')
#score
status_str = StringVar()
status_str.set('Score '+str(score))
show_status = Label(window, textvariable=status_str)
show_status.pack(pady=20)
#question
question_str = StringVar()
question_str.set(ask)
question_str_label = Label(window, textvariable=question_str)
question_str_label.pack(pady=25)
#answer box
textBox=Text(window, height=1, width=25, font=(100), borderwidth=(10))
textBox.pack(pady=10)
#submit button
buttonsubmit=Button(window, height=1, width=10, text="Submit", command=lambda: retrieve_input())
buttonsubmit.pack(pady=10)
#text
text_str = StringVar()
text_str.set(text)
text_str = Label(window, textvariable=text_str, font=(28))
text_str.pack(pady=30)
window.mainloop()
使用
.config(text=)
您对StringVar
和Label
使用了相同的名称question\u var
,因此question\u str
最终是Label
。为标签使用不同的名称。在text\u str
上也有同样的问题。你为什么要使用StringVar
s呢?使用.config(…)
可以更改标签的所有属性。
from tkinter import *
import random
import time
End = False
score=0
question=['13**2','5+1','60*2']
random.shuffle(question)
ask=question.pop()
text = ('text')
#command
def retrieve_input():
global ans,score,ask
if len(question)!=0:
ans=textBox.get("1.0","end-1c")
print(ans)
if int(ans) == int(eval(ask)):
score=int(score)+1
status_str.set('Score '+str(score))
random.shuffle(question)
ask=question.pop()
question_str.set(ask)
return score,ask
#app window
window = Tk()
window.title('Math Problem')
window.geometry('700x400')
#score
status_str = StringVar()
status_str.set('Score '+str(score))
show_status = Label(window, textvariable=status_str)
show_status.pack(pady=20)
#question
question_str = StringVar()
question_str.set(ask)
question_str_label = Label(window, textvariable=question_str)
question_str_label.pack(pady=25)
#answer box
textBox=Text(window, height=1, width=25, font=(100), borderwidth=(10))
textBox.pack(pady=10)
#submit button
buttonsubmit=Button(window, height=1, width=10, text="Submit", command=lambda: retrieve_input())
buttonsubmit.pack(pady=10)
#text
text_str = StringVar()
text_str.set(text)
text_str = Label(window, textvariable=text_str, font=(28))
text_str.pack(pady=30)
window.mainloop()