【问题标题】:How to enhance quiz game for multiple choice?如何增强多项选择的问答游戏?
【发布时间】:2018-09-03 11:44:15
【问题描述】:

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

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

这是我当前的代码:

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

【问题讨论】:

  • 看起来您已将“问题一”固定在窗口底部。并且不接受它作为答案xD
  • 哦,对不起,我是新来的。不知道事情是如何运作的。对不起,我的坏人。我会接受你的回答!
  • 我将处理一个示例并在几分钟内发布解决方案。
  • 好。但为了记录:“请帮助我”不是这个社区范围内的请求。为了更符合这个社区的政策,你应该先尝试自己解决这个问题。然后,当您遇到具体问题时,您应该提出一个问题。放下需求和一些代码,然后:现在告诉我如何将这两件事结合在一起......正如所说:不是一个好主意。
  • 可以理解。我会更符合社区规则和准则。这个问题一直困扰着我一段时间,我尝试自己解决但失败了,所以我将尝试在这里提问,

标签: python tkinter


【解决方案1】:

你去吧,这应该为你打下良好的工作基础。我添加了一些 cmets 来帮助你。我保留了您的大部分代码,以便您可以轻松理解。请注意,这不是编写优秀问答游戏的最佳方式。但是,如果您开始使用它,那也没关系。 可以使用大量 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

【讨论】:

  • 如果您遇到问题请告诉我
  • 好的!我会以此为基础!这会给我一个开始。谢谢大佬!
  • 嘿 Vineeth,我从你的基础开始,但我遇到了一个问题,它说“Tkinter 回调回溯中的异常(最近一次调用最后一次):文件“C:\Users \xxx\AppData\Local\Programs\Python\Python36-32\lib\tkinter_init_.py",第 1702 行,在 call 中返回 self.func(*args)类型错误:new_window() 缺少 1 个必需的位置参数:“问题””
  • 好的,我已经编辑了代码以从 new_window() 中删除问题参数。做检查:)
  • 在这里,我更改了您犯的一些错误。还有一些你应该可以自己修复的:) pastebin.com/JdsyVJR0
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-01
  • 1970-01-01
相关资源
最近更新 更多