Python tkinter:输入不等于回答?

时间:2015-03-17 08:08:55

标签: python tkinter

我使用Tkinterpython使用此代码,当我正确回答时,它会说答案是错误的:

    self.whatWord = StringVar()

    self.missingWordPrompt = Entry(root, textvariable=self.whatWord)
    self.missingWordPrompt.pack()

    #self.missingWordPrompt.bind('<Return>', self.checkWord())

    self.submitbutton = Button(root, text='Check Answer', command=self.checkWord)
    self.submitbutton.pack()

    '''self.answerLabel = Label(root, textvariable=self.missingWord)
    self.answerLabel.pack()'''

    self.answer = self.missingWord.get()

    print(self.answer)

def checkWord(self):
    self.my_input = self.missingWordPrompt.get()
    print(self.my_input)

    if str(self.answer).lower() == str(self.my_input).lower():
        print('correct')
    else:
        print('wrong answer')

在控制台中我得到了这个:

Missing word: Football
football
wrong answer
football
wrong answer

1 个答案:

答案 0 :(得分:1)

您必须使用StringVar实例操作,例如:

        self.whatWord = StringVar()
        self.missingWordPrompt = Entry(root, textvariable=self.whatWord)
        self.missingWordPrompt.pack()

        self.submitbutton = Button(root, text='Check Answer', command=self.checkWord)
        self.submitbutton.pack()
def checkWord(self):
    self.answer = self.missingWord.get()
    self.my_input = self.whatWord.get()
    print(self.answer, self.my_input)

    if str(self.answer).strip().lower() == self.my_input.strip().lower():
        print('correct')
    else:
        print('wrong answer')

关于Tkinter.Entry()

,请阅读this manual