1

コードは次のとおりです。

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 エントリ ボックスを使用してユーザーからの入力を取得するクイズ ゲームを作成しようとしていますが、オブジェクトに属性 get がないことがわかります。助けてください!

4

2 に答える 2

1

この行が関数の外で定義されたグローバル名に影響を与えることが予想される場合AnswerEntry = Entry(root)は、関数内でそれをグローバルとして宣言する必要がありますStartGame()

global AnswerEntry
AnswerEntry = Entry(root)

関数内の変数への代入は、その変数名を関数に対してのみローカルにします。AnswerEntry他の場所でグローバルに整数値を割り当てたようにsubmit_answer()見えるので、 を呼び出すとそれがわかりますAnswerEntry.get()

ただし、グローバルは本当に避けるべきです。

于 2013-02-08T10:46:53.877 に答える
0

AnswerEntryは整数であり、オブジェクトではないため、そのメソッドを呼び出すことはできません。

たぶん、オブジェクトインスタンスがありませんか?

于 2013-02-08T10:38:48.597 に答える