如何使用按钮在Tkinter中创建测验

2024-04-19 00:07:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图得到一个小测验,问另一个问题,并加载与该问题有关的答案,一旦一个按钮被按下。它加载第一个问题,一个按钮有正确的答案,三个按钮有错误的答案(这是假设这样做,因为它是多项选择),但当一个按钮被按下时,新的问题不会加载。它说当我按下一个按钮时,ID没有被定义。你知道吗

所以我怎么循环它,这样一个新的ID出现了,所以一个新的问题负载。多项选择的工作,它只是一个循环回。你知道吗

class Page2(Page):
    def __init__(self, *args, **kwargs):
        Page.__init__(self, *args, **kwargs)
        self.Question_Counter = 0
        self.Correct_Answers = 0
        self.getQuest
        self.AnswerQuestion

        self.ID = random.randint(1,100)
        QuestionSet.add(self.ID)
        self.QuestionAsked = self.getQuest(self.ID)
        QuestionSet.add(self.QuestionAsked)
        Answers = [self.GetAnswer(self.ID)]
        self.CorrectAnswer = self.GetAnswer(self.ID)
        for ID in range(3):
            new_id = random.randint(1,100)
            while new_id in Answers:
                new_id = random.randint(1,100)
            Answers.append(self.GetAnswer(new_id))
        self.OriginalAnswers = Answers[:]
        print(self.CorrectAnswer)
        print(self.GetAnswer(self.ID))

        l1 = tk.Label(self, text = self.QuestionAsked)
        l1.grid(row = 2, column = 2, pady = 100)

        l2 = tk.Label(self, text ="/50")
        l2.grid(row = 1, column = 3)

        myID = random.randint(0,3)
        b1 = tk.Button(self, text = Answers[myID], command = self.AnswerQuestion)
        b1.grid(row = 3, column = 1)
        del Answers[myID]

        myID = random.randint(0,2)
        b2 = tk.Button(self, text = Answers[myID], command = self.AnswerQuestion)
        b2.grid(row = 4, column = 1, pady = 10)
        del Answers[myID]

        myID = random.randint(0,1)
        b3 = tk.Button(self, text = Answers[myID], command = self.AnswerQuestion)
        b3.grid(row = 3, column = 3)
        del Answers[myID]

        b4 = tk.Button(self, text = Answers[0], command = self.AnswerQuestion)
        b4.grid(row = 4, column = 3, pady = 10)
        del Answers[0]

    def getQuest(self,ID):
        if self.Question_Counter < 50:
            RandomInt = random.randint(1,100)
            new_db = sqlite3.connect("QuestionsDatabase.db")
            c= new_db.cursor()
            return (c.execute("SELECT Question FROM Questions WHERE QuestionID = {}".format(ID)).fetchone()[0])
            self.Loading(self)

    def GetAnswer(self,ID):
        new_db = sqlite3.connect("QuestionsDatabase.db")
        c= new_db.cursor()
        return (c.execute("SELECT ANSWER FROM ANSWERS WHERE AnswerID = {}".format(ID)).fetchone()[0])  

    def AnswerQuestion(self):
        if self.CorrectAnswer == self.GetAnswer(self.ID):
            self.Correct_Answers += 1
        self.Question_Counter += 1
        self.getQuest(self,ID)

Tags: textselfidnewdbcolumnrandomtk