Тест Tkinter не заканчивается, когда он достигает 10 - PullRequest
0 голосов
/ 02 мая 2018
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 и т. Д. У меня есть только 10 вопросов в базе данных SQL, из которой я их вызываю, но просто повторяю каждый вопрос дважды.

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 голосов
/ 02 мая 2018

x объявлен как IntVar, чтобы получить его значение, вы должны использовать его метод 'get' как:

if x.get() == 10:
    # DO SOMETHING
else:
    # DO SOMETHING ELSE
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...