1

Here is the code:

def StartGame():
    root = Tk()
    root.title("Maths Quiz - Trigonometry and Pythagoras' Theorem | Start The Game")
    root.geometry("640x480")
    root.configure(background = "gray92")
    TotScore = 0
    Count = 0
    while Count < 10:
        AnswerReply = None
        WorkingArea = Text(root, width = 70, height = 10, wrap = WORD).place(x = 38, y = 100)
        n = GetRandomNumber
        Question,RealAnswer = QuestionLibrary(Opposite,Adjacent,Hypotenuse,Angle,n)
        AskQuestion = Label(root, text = Question).place(x = 38, y = 300)
        PauseButton = ttk.Button(root, text = "Pause").place(x = 380, y = 10)
        HelpButton = ttk.Button(root, text = "Help", command = helpbutton_click).place(x = 460, y = 10)
        QuitButton = ttk.Button(root, text = "Quit", command = root.destroy).place(x = 540, y = 10)
        AnswerEntry = Entry(root)
        AnswerEntry.place(x = 252, y = 375)
        SubmitButton = ttk.Button(root, text = "Submit", command = submit_answer).place(x = 276, y = 400)
        Count += 1
    root.mainloop()

This is the function used with the submit button:

def submit_answer():
    Answer = AnswerEntry.get()
    print(Answer)
    TotScore,AnswerReply = IsAnswerCorrect(Answer,RealAnswer)
    ScoreLabel = ttk.Label(root, text = TotScore).place(x = 10, y = 10)
    AnswerReplyLabel = ttk.Label(root, text = AnswerReply).place(x = 295, y = 440)

And this is the error I get when I click on the SubmitButton

Traceback (most recent call last):
  File "C:\Python32\lib\tkinter\__init__.py", line 1399, in __call__
    return self.func(*args)
  File "C:\Users\ANNIE\Documents\School\Computing\Project\Python\GUI Maths Quiz.py", line 178, in submit_answer
    Answer = AnswerEntry.get()
AttributeError: 'int' object has no attribute 'get'

I'm trying to make a quiz game where I get an input from the user using the AnswerEntry Entry box, however it is telling me that the object has no attribute get, please help!

3
  • Is AnswerEntry a global? You don't declare it as such in StartGame(). Commented Feb 8, 2013 at 10:41
  • I've defined it outside of the function, but I haven't put "global AnswerEntry" anywhere... Commented Feb 8, 2013 at 10:43
  • Okay I set AnswerEntry as a global variable and it worked, thank you Martijn! Commented Feb 8, 2013 at 10:45

2 Answers 2

1

If you expect the AnswerEntry = Entry(root) line to affect a global name defined outside of the function, you need to declare it a global inside of your StartGame() function:

global AnswerEntry
AnswerEntry = Entry(root)

Assignment to a variable in a function makes that variable name local to the function only. It appears you assigned an integer value to the global AnswerEntry elsewhere, so submit_answer() sees that when you call AnswerEntry.get().

You should really avoid globals though.

Sign up to request clarification or add additional context in comments.

1 Comment

+1. And you should definitely avoid reusing globals to mean an integer sometimes, an Entry other times…
0

AnswerEntry is an intenger and not an object, so you can't call that method on him.

Maybe you're missing object instance?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.