Tkinter测验在达到10时不会结束

时间:2018-05-02 12:26:32

标签: python tkinter sqlite

def confirmAnswer(self):
        answer = self.enterAnswer.get() #Obtains what the user has typed into the textbox.
        if answer == self.correctAnswer: #Checks the answer submitted compares to correct answer.
            self.quizScore += 1 # Adds one to score.
            ms.showinfo(title='Correct', message='Correct')
            #self.Qn = self.Qn + 1# This changes to the next question.

            if x == 10:# Question number is one above the number of questions else last question wont work. Ends when it hits 11 as 10 questions.
                ms.showinfo(title="End of test", message = "This is the end of the test. You scored")
            else:
                self.update_question() # Gets next question

        else:
            ms.showwarning(title='Incorrect', message='Incorrect')
            if x == 10:# Question number is one above the number of questions else last question wont work. Ends when it hits 11 as 10 questions.
                ms.showinfo(title="End of test", message = "This is the end of the test. You scored")
            else:
                self.update_question()
                global num
                num = randint(1,10) 

测验应该在问题10结束时结束,但继续问题11,12等。我在SQL数据库中只有10个问题,我从中调用它们,但只是重复每个问题两次。

def update_question(self):
        # Get new question
        global num
        num = randint(1, 10)
        query = "SELECT * FROM questions WHERE [qnumber] =?"
        c.execute(query,(num,))
        row = c.fetchone()

        self.question['text'] = row[1]

        self.answer1['text'] = row[2]
        self.answer2['text'] = row[3]
        self.answer3['text'] = row[4]
        self.answer4['text'] = row[5]

        self.correctAnswer = row[6] 

这是更新问题的代码段。

global x
x = IntVar()
x.set(1)
def add():
    x.set(x.get()+1)

self.recordNum = Label(self.quizf, textvariable=x)
self.recordNum.pack()

self.enterAnswer = Entry(self.quizf)
self.Submit = Button(self.quizf, text = 'submit', command=lambda:[self.confirmAnswer(),add()])

这段代码是按下提交按钮时执行的问题编号更新的方式。当x达到10时,它应该结束代码但继续添加1.

1 个答案:

答案 0 :(得分:2)

x被声明为IntVar,要检索其值,您应该使用其方法'get'作为:

if x.get() == 10:
    # DO SOMETHING
else:
    # DO SOMETHING ELSE