微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

问:为什么 'Label' 对象没有属性 'set'

如何解决问:为什么 'Label' 对象没有属性 'set'

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

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

检测到错误

第 25 行,在retrieve_input 中 ..... question_str.set(ask) ..... AttributeError: 'Label' 对象没有属性 '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,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()

解决方法

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。