Jibhong
Jibhong

Reputation: 13

Q: Why 'Label' object has no attribute 'set'

This is my math game but, when 'ask' str is update its error and question isn't change.

I try to solved it by change the code position but it didn't work.

Error detected:

line 25, in 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()

Upvotes: 0

Views: 2203

Answers (1)

Ali Saad
Ali Saad

Reputation: 110

what happened is not a big deal just try to make a different names for each item because python confused for getting from the label I tried the code and it worked successfully the code:

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

Upvotes: 1

Related Questions