diff --git a/questions.db b/questions.db new file mode 100644 index 0000000..8dc7359 Binary files /dev/null and b/questions.db differ diff --git a/quiz.py b/quiz.py new file mode 100644 index 0000000..dca47d5 --- /dev/null +++ b/quiz.py @@ -0,0 +1,141 @@ +import sqlite3 +import random +import tkinter as tk +from tkinter import messagebox +import time +import ctypes # Modul für Monitorauflösung + +# Funktion, um die Monitorauflösung zu bekommen +user32 = ctypes.windll.user32 +screen_width, screen_height = user32.GetSystemMetrics(0), user32.GetSystemMetrics(1) + +# Datenbankverbindung herstellen +conn = sqlite3.connect('questions.db') # Hier Änderung auf 'questions.db' +cursor = conn.cursor() + +# Fragen und Antworten aus der Datenbank abrufen +cursor.execute("SELECT * FROM py_questions") +all_questions = cursor.fetchall() + +# Maximal 20 Fragen zufällig auswählen +questions = random.sample(all_questions, min(len(all_questions), 20)) + +class QuizApp: + def __init__(self, root): + self.root = root + self.root.title("Python Quiz") + + # Fenstergröße auf 80% der aktuellen Monitorauflösung setzen + window_width = int(screen_width * 0.8) + window_height = int(screen_height * 0.8) + self.root.geometry(f"{window_width}x{window_height}") + + self.start_button = tk.Button(root, text="Start Quiz", command=self.start_quiz) + self.start_button.pack(pady=20) + + self.question_label = tk.Label(root, text="", wraplength=int(window_width * 0.9)) + self.question_label.pack(pady=20) + self.question_label.pack_forget() + + self.answers_frame = tk.Frame(root) + self.answers_frame.pack(pady=20) + self.answers_frame.pack_forget() + + self.answer_buttons = [] + self.checkbuttons = [] + + for i in range(4): + var = tk.IntVar() + cb = tk.Checkbutton(self.answers_frame, text="", variable=var) + cb.pack(anchor='w') + cb.pack_forget() + self.checkbuttons.append(cb) + rb = tk.Button(self.answers_frame, text="", command=lambda idx=i: self.check_answer_single(idx)) + rb.pack(anchor='w') + rb.pack_forget() + self.answer_buttons.append(rb) + + self.submit_button = tk.Button(root, text="Submit", command=self.check_answer) + self.submit_button.pack(pady=20) + self.submit_button.pack_forget() + + self.next_button = tk.Button(root, text="Next", command=self.load_question) + self.next_button.pack(pady=20) + self.next_button.pack_forget() + + self.timer_label = tk.Label(root, text="") + self.timer_label.pack(pady=10) + self.timer_label.pack_forget() + + self.score = 0 + self.current_question = -1 + self.start_time = None + + def start_quiz(self): + self.start_button.pack_forget() + self.question_label.pack() + self.answers_frame.pack() + self.submit_button.pack() + self.next_button.pack() + self.timer_label.pack() + self.start_time = time.time() + self.update_timer() + self.load_question() + + def update_timer(self): + if self.start_time: + elapsed_time = int(time.time() - self.start_time) + self.timer_label.config(text=f"Time: {elapsed_time}s") + self.root.after(1000, self.update_timer) + + def load_question(self): + self.current_question += 1 + if self.current_question >= len(questions): + elapsed_time = int(time.time() - self.start_time) + messagebox.showinfo("Quiz Finished", f"Your score: {self.score}/{len(questions)}\nTime: {elapsed_time}s") + self.root.destroy() + return + + question = questions[self.current_question] + self.question_label.config(text=question[3]) + + # Antworten löschen + for cb in self.checkbuttons: + cb.pack_forget() + for rb in self.answer_buttons: + rb.pack_forget() + + if isinstance(question[9], tuple): # Multiple correct answers + for i in range(4): + if i < len(question[9]): + self.checkbuttons[i].config(text=question[4 + i], variable=tk.IntVar()) + self.checkbuttons[i].pack(anchor='w') + else: + self.checkbuttons[i].pack_forget() + self.answer_buttons[i].pack_forget() + self.submit_button.config(state=tk.NORMAL) + else: # Single correct answer + for i in range(4): + self.checkbuttons[i].pack_forget() + self.answer_buttons[i].config(text=question[4 + i], command=lambda idx=i: self.check_answer_single(idx)) + self.answer_buttons[i].pack() + + def check_answer(self): + selected_answers = [cb.cget("text") for cb in self.checkbuttons if cb.instate(['selected'])] + correct_answers = [answer for i, answer in enumerate(questions[self.current_question][4:8]) if i + 1 in map(int, questions[self.current_question][9].split(','))] + + if set(selected_answers) == set(correct_answers): + self.score += 1 + self.load_question() + + def check_answer_single(self, idx): + if idx == int(questions[self.current_question][9]): + self.score += 1 + self.load_question() + +root = tk.Tk() +app = QuizApp(root) +root.mainloop() + +# Verbindung schließen +conn.close()