0

这是我的数学游戏,但是当'ask'str 更新时,它的错误和问题没有改变。

我尝试通过更改代码位置来解决它,但它没有用。

检测到错误:

第 25 行,在retrieve_input ..... question_str.set(ask) ..... AttributeError: 'Label' object has no attribute 'set'

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(window, textvariable=question_str)
question_str.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()
4

1 回答 1

1

发生的事情没什么大不了的,只是尝试为每个项目取一个不同的名称,因为 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()

于 2021-05-03T21:29:02.700 回答