from tkinter import *
from tkinter import messagebox as mb
# Definindo as perguntas, respostas e opções de resposta diretamente no código Python
perguntas = [
"Q1. Qual é o cognome do D.João VI ?",
"Q2. Qual é o cognome do D.Dinis I?",
"Q3. Qual é o cognome do D.João II?",
"Q4. Qual é o cognome do D.João V?"
]
respostas_corretas = [1, 2, 3, 2]
opcoes_resposta = [
["O Clemente", "O Africano", " O Reformado", "O Vitorioso"],
["O Gordo", " O Lavrador", "O Povoador", "O Casto"],
["O Diplomata", "O Esperançoso", "O Príncipe Perfeito", "O Magnânimo"],
["O Prudente", "O Magnânimo", "O Popular", "O Pacífico"]
]
class Quiz:
def __init__(self):
self.q_no = 0
self.display_title()
self.display_question()
self.opt_selected = IntVar()
self.opts = self.radio_buttons()
self.display_options()
self.buttons()
self.data_size = len(perguntas)
self.correct = 0
def display_result(self):
wrong_count = self.data_size - self.correct
correcto = f"Correcto: {self.correct}"
errado = f"Errado: {wrong_count}"
score = int(self.correct / self.data_size * 100)
resultado = f"Pontos: {score}%"
mb.showinfo("Resultado", f"{resultado}\n{correcto}\n{errado}")
def check_ans(self, q_no):
if self.opt_selected.get() == respostas_corretas[q_no]:
return True
def next_btn(self):
if self.check_ans(self.q_no):
self.correct += 1
self.q_no += 1
if self.q_no == self.data_size:
self.display_result()
gui.destroy()
else:
self.display_question()
self.display_options()
def buttons(self):
next_button = Button(root, text="Next", command=self.next_btn,
width=10, bg="blue", fg="white", font=("ariel", 16, "bold"))
next_button.place(x=350, y=380)
quit_button = Button(root, text="Quit", command=root.destroy,
width=5, bg="black", fg="white", font=("ariel", 16, " bold"))
quit_button.place(x=700, y=50)
def display_options(self):
val = 0
self.opt_selected.set(0)
for option in opcoes_resposta[self.q_no]:
self.opts[val]['text'] = option
val += 1
def display_question(self):
q_no = Label(root, text=perguntas[self.q_no], width=60,
font=('ariel', 16, 'bold'), anchor='w')
q_no.place(x=70, y=100)
def display_title(self):
title = Label(root, text="Perguntas sobre reis",
width=50, bg="green", fg="white", font=("ariel", 20, "bold"))
title.place(x=0, y=2)
def radio_buttons(self):
q_list = []
y_pos = 150
while len(q_list) < 4:
radio_btn = Radiobutton(root, text=" ", variable=self.opt_selected,
value=len(q_list)+1, font=("ariel", 14))
q_list.append(radio_btn)
radio_btn.place(x=100, y=y_pos)
y_pos += 40
return q_list
root = Tk()
root.geometry("800x450")
root.title("Quiz")
quiz = Quiz()
root.mainloop()