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

2024-09-29 01:36:40 发布

您现在位置:Python中文网/ 问答频道 /正文

代码如下:

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

这是与“提交”按钮一起使用的功能:

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)

这就是我点击submit按钮时出现的错误

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'

我试图做一个问答游戏,我从用户那里得到一个输入使用答案输入框,但它告诉我,对象没有属性get,请帮助!


Tags: textanswergetcountplacebuttonrootlabel
2条回答

AnswerEntry是一个intenger而不是一个对象,因此不能对他调用该方法。

也许你丢失了对象实例?

如果希望AnswerEntry = Entry(root)行影响在函数外部定义的全局名称,则需要在StartGame()函数内部将其声明为全局名称:

global AnswerEntry
AnswerEntry = Entry(root)

对函数中某个变量的赋值只会使该变量名成为函数的本地变量。似乎您在其他地方为全局AnswerEntry分配了一个整数值,所以submit_answer()在调用AnswerEntry.get()时会看到这一点。

不过,你真的应该避免全球变暖。

相关问题 更多 >