94 lines
3.1 KiB
Python
94 lines
3.1 KiB
Python
import tkinter as tk
|
|
from tkinter import messagebox
|
|
import sqlite3
|
|
import csv
|
|
import random
|
|
|
|
class QuizApp:
|
|
def __init__(self, root):
|
|
self.root = root
|
|
self.root.title("Karteikarten Quiz")
|
|
|
|
self.questions = []
|
|
self.current_question = 0
|
|
self.score = 0
|
|
|
|
self.load_questions()
|
|
|
|
self.question_label = tk.Label(root, text="", wraplength=300)
|
|
self.question_label.pack(pady=20)
|
|
|
|
self.options = tk.StringVar()
|
|
self.option_buttons = []
|
|
for _ in range(4):
|
|
rb = tk.Radiobutton(root, text="", variable=self.options, value="")
|
|
rb.pack(anchor=tk.W)
|
|
self.option_buttons.append(rb)
|
|
|
|
self.submit_button = tk.Button(root, text="Antworten", command=self.check_answer)
|
|
self.submit_button.pack(pady=10)
|
|
|
|
self.score_label = tk.Label(root, text="Punkte: 0")
|
|
self.score_label.pack(pady=20)
|
|
|
|
self.show_question()
|
|
|
|
def load_questions(self):
|
|
# Wählen Sie zwischen CSV oder SQLite
|
|
use_csv = True
|
|
|
|
if use_csv:
|
|
self.load_questions_from_csv("questions.csv")
|
|
else:
|
|
self.load_questions_from_sqlite("quiz.db")
|
|
|
|
def load_questions_from_csv(self, filename):
|
|
with open(filename, newline='', encoding='utf-8') as csvfile:
|
|
reader = csv.reader(csvfile)
|
|
for row in reader:
|
|
if row:
|
|
question, correct_answer, *options = row
|
|
self.questions.append((question, correct_answer, options))
|
|
|
|
def load_questions_from_sqlite(self, dbname):
|
|
conn = sqlite3.connect(dbname)
|
|
cursor = conn.cursor()
|
|
cursor.execute("SELECT question, correct_answer, option1, option2, option3, option4 FROM questions")
|
|
rows = cursor.fetchall()
|
|
for row in rows:
|
|
question = row[0]
|
|
correct_answer = row[1]
|
|
options = row[2:]
|
|
self.questions.append((question, correct_answer, options))
|
|
conn.close()
|
|
|
|
def show_question(self):
|
|
if self.current_question < len(self.questions):
|
|
question, correct_answer, options = self.questions[self.current_question]
|
|
self.question_label.config(text=question)
|
|
random.shuffle(options)
|
|
for rb, option in zip(self.option_buttons, options):
|
|
rb.config(text=option, value=option)
|
|
self.options.set(None)
|
|
else:
|
|
self.end_quiz()
|
|
|
|
def check_answer(self):
|
|
if self.current_question < len(self.questions):
|
|
_, correct_answer, _ = self.questions[self.current_question]
|
|
user_answer = self.options.get()
|
|
if user_answer == correct_answer:
|
|
self.score += 1
|
|
self.current_question += 1
|
|
self.score_label.config(text=f"Punkte: {self.score}")
|
|
self.show_question()
|
|
|
|
def end_quiz(self):
|
|
messagebox.showinfo("Quiz Ende", f"Du hast {self.score} von {len(self.questions)} Punkten erreicht!")
|
|
self.root.quit()
|
|
|
|
if __name__ == "__main__":
|
|
root = tk.Tk()
|
|
app = QuizApp(root)
|
|
root.mainloop()
|