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_widgets = [] # Liste für Widgets (Buttons oder Checkbuttons) 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]) # Alle vorhandenen Antwort-Widgets aus dem Frame entfernen for widget in self.answer_widgets: widget.pack_forget() # Je nach Art der richtigen Antwort(en) die Antwortmöglichkeiten anzeigen if isinstance(question[9], tuple): # Mehrere richtige Antworten for i in range(4): if i < len(question[4:8]): var = tk.IntVar() cb = tk.Checkbutton(self.answers_frame, text=question[4 + i], variable=var) cb.pack(anchor='w') self.answer_widgets.append((cb, var)) else: break else: # Eine richtige Antwort for i in range(4): if i < len(question[4:8]): rb = tk.Button(self.answers_frame, text=question[4 + i], command=lambda idx=i: self.check_answer_single(idx)) rb.pack(anchor='w') self.answer_widgets.append(rb) else: break # Buttons für Submit und Next anzeigen self.submit_button.pack() self.next_button.pack() def check_answer(self): selected_answers = [] for widget in self.answer_widgets: if isinstance(widget, tuple): # Checkbutton cb, var = widget if var.get() == 1: selected_answers.append(cb.cget("text")) elif isinstance(widget, tk.Button): # Button selected_answers.append(widget.cget("text")) # Richtige Antworten aus der Datenbank abrufen correct_answers_db = questions[self.current_question][9] if isinstance(correct_answers_db, tuple): correct_answers = [answer for i, answer in enumerate(questions[self.current_question][4:8]) if i + 1 in correct_answers_db] else: correct_answers = [correct_answers_db] # Antworten vergleichen if set(selected_answers) == set(correct_answers): self.score += 1 self.load_question() def check_answer_single(self, idx): correct_answer = questions[self.current_question][9] if isinstance(correct_answer, int): if idx == correct_answer - 1: # Index in der Datenbank beginnt bei 1 self.score += 1 else: # Text als Antwort, überprüfen, ob die ausgewählte Antwort mit der richtigen übereinstimmt if questions[self.current_question][4 + idx] == correct_answer: self.score += 1 self.load_question() root = tk.Tk() app = QuizApp(root) root.mainloop() # Verbindung schließen conn.close()