Вот, пожалуйста, это должно создать вам хорошую базу для работы. Я добавил несколько комментариев, чтобы помочь вам. Я сохранил большую часть вашего кода без изменений, чтобы вы могли легко понять. Обратите внимание, что это не самый лучший способ написать хорошую игру. Но хорошо, если вы начинаете это.
Оценка может быть выполнена с использованием множества операторов 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