2013-02-08 79 views
1

下面是代碼:AttributeError的:「詮釋」對象有沒有屬性「得到」

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) 

這是錯誤我得到當我點擊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' 

我想做一個測驗遊戲,我從用戶使用AnswerEntry條目但是它告訴我該對象沒有屬性,請幫忙!

+0

「AnswerEntry」是全球性的嗎?你不要在'StartGame()'中聲明它。 –

+0

我定義它的功能之外,但我還沒有把「全球AnswerEntry」任何地方...... –

+0

好吧,我設置AnswerEntry作爲一個全局變量,它的工作,謝謝馬亭! –

回答

1

如果您期望AnswerEntry = Entry(root)線影響功能之外定義一個全局命名,你需要聲明它的StartGame()函數的全局內:

global AnswerEntry 
AnswerEntry = Entry(root) 

賦值給一個變量的函數,使該只有函數本地的變量名。看起來你在其他地方爲全局AnswerEntry分配了一個整數值,所以submit_answer()在你撥打AnswerEntry.get()時會看到。

儘管你應該避免使用全局變量。

+1

+1。你應該_definitely_避免重複使用全局變量來表示一個整數有時候,一個'Entry'有時候...... – abarnert

0

AnswerEntry是一個整數,而不是一個對象,所以你不能在他身上調用該方法。

也許你缺少對象實例?

相關問題