简体   繁体   English

AttributeError:“ int”对象没有属性“ get”

[英]AttributeError: 'int' object has no attribute 'get'

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 这是我单击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! 我正在尝试做一个问答游戏,使用AnswerEntry Entry框从用户那里获得输入,但是它告诉我该对象没有属性get,请帮忙!

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: 如果您希望AnswerEntry = Entry(root)行会影响在函数外部定义的全局名称,则需要在StartGame()函数内部将其声明为全局名称:

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() . 似乎您为其他地方的全局AnswerEntry分配了一个整数值,因此当您调用AnswerEntry.get()时, submit_answer()看到这一点。

You should really avoid globals though. 但是,您应该真正避免使用全局变量。

AnswerEntry is an intenger and not an object, so you can't call that method on him. AnswerEntry是一个整数,而不是一个对象,因此您不能在他身上调用该方法。

Maybe you're missing object instance? 也许您缺少对象实例?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM