如何增强测验游戏的选择范围?

时间:2018-09-03 11:44:16

标签: python tkinter

我不确定如何将其创建为“多项选择”问答游戏。

我不知道如何继续。我不确定如何在这种情况下使用类。如果我能得到第一个带有答案的问题,我敢肯定我会继续从那里继续,但是我需要帮助。

这是我当前的代码:

from tkinter import *
from tkinter import ttk


#User Interface Code

root = Tk() # Creates the window
root.title("Quiz Game")

def new_window():
   newWindow = Toplevel()
   message = Label(newWindow, text="What is the capital of France?", font = 
   ("Arial", "24"), pady=30)
   display = Label(newWindow, width=150, height=40)

   class letsQuiz:
      def __init__(self, questions, answers):
         self.questions = questions
         self.answers = answers

         self.label = Label(newWindow, text="What is the capital of France?")
         self.label.pack()

         self.answer1button = Button(newWindow, text="Madrid")
         self.answer2button = Button(newWindow, text="Paris")
         self.answer3button = button(newWindow, text="London")

         self.answer1button.pack()
         self.answer2button.pack()
         self.answer3button.pack()

   message.pack()
   display.pack()

message_label1 = Label(text="Let's quiz some of your knowledge :)", font = ( 
"Arial", "25"), padx=40, pady=20)
message_label2 = Label(root, text="Click 'Continue' to begin.", 
wraplength=250)
button1 = Button(root, text ="Continue", command=new_window, width=16, 
bg="red")
display2 = Label(root, width=100, height=30)

message_label1.pack()
message_label2.pack()
button1.pack()
display2.pack()

root.mainloop() # Runs the main window loop

1 个答案:

答案 0 :(得分:2)

您去了,这应该为您奠定了良好的基础。我添加了一些评论来帮助您。我已将大多数代码保持完整,以便您轻松理解。请注意,这不是编写优秀问答游戏的最佳方法。但是好吧,如果您正在开始。 可以使用许多if语句和实例变量来进行评分。祝你好运!

from tkinter import *
from tkinter import ttk

#User Interface Code

root = Tk() # Creates the window
root.title("Quiz Game")


def new_window():
   newWindow = Toplevel()
   message = Label(newWindow, font = 
   ("Arial", "24"), pady=30)
   display = Label(newWindow, width=150, height=40)
   return newWindow

class letsQuiz:

    def __init__(self, window):
        self.newWindow = window

        self.question_counter = 0


        # Add your questions and answers here
        self.questions = ['# QUESTION: 1', '# QUESTION: 2', '# QUESTION: 3', '# QUESTION: 4']
        # Each list is a set of answers corresponding to question
        self.answers = [['question_1_Answer_1', 'question_1_Answer_2', 'question_1_Answer_3'], 
                        ['question_2_Answer_1', 'question_2_Answer_2', 'question_2_Answer_3'],
                        ['question_3_Answer_1', 'question_3_Answer_2', 'question_3_Answer_3'],
                        ['question_4_Answer_1', 'question_4_Answer_2', 'question_4_Answer_3']]

        self.label = Label(self.newWindow, text=self.questions[self.question_counter], font = 
        ("Arial", "24"), pady=30)


        self.answer1button = Button(self.newWindow, text=self.answers[self.question_counter][0])
        self.answer2button = Button(self.newWindow, text=self.answers[self.question_counter][1])
        self.answer3button = Button(self.newWindow, text=self.answers[self.question_counter][2])

        self.nextButton = Button(self.newWindow, text="Next", command=self.next_question)

    def pack_all(self):
        '''
        Packs all widgets into the window
        '''
        self.label.pack()

        self.answer1button.pack()
        self.answer2button.pack()
        self.answer3button.pack()

        self.nextButton.pack()

    def forget_all(self):
        '''
        Removes all widgets from the window
        '''
        self.label.pack_forget()

        self.answer1button.pack_forget()
        self.answer2button.pack_forget()
        self.answer3button.pack_forget()

        self.nextButton.pack_forget()


    def next_question(self):
        '''
        Updates the question and its corresponding answers
        '''
        self.question_counter += 1

        self.forget_all() # remove old question 


        try:

            self.label = Label(self.newWindow, text=self.questions[self.question_counter], font = 
            ("Arial", "24"), pady=30)


            self.answer1button = Button(self.newWindow, text=self.answers[self.question_counter][0])
            self.answer2button = Button(self.newWindow, text=self.answers[self.question_counter][1])
            self.answer3button = Button(self.newWindow, text=self.answers[self.question_counter][2])

            self.nextButton = Button(self.newWindow, text="Next", command=self.next_question)
        except IndexError:
            self.forget_all()
            msg = Label(self.newWindow, text="You have completed the quiz Thank you for playing, Close to EXIT", font = 
            ("Arial", "24"), pady=30)
            msg.pack()




        self.pack_all() # place in the new question    

quiz = letsQuiz(new_window() )

#message.pack()
#display.pack()

message_label1 = Label(text="Let's quiz some of your knowledge :)", font = ( 
"Arial", "25"), padx=40, pady=20)
message_label2 = Label(root, text="Click 'Continue' to begin.", 
wraplength=250)
button1 = Button(root, text ="Continue", command=quiz.pack_all, width=16, 
bg="red")
display2 = Label(root, width=100, height=30)

message_label1.pack()
message_label2.pack()
button1.pack()
display2.pack()

root.mainloop() # Runs the main window loop