From 67546c65927b23773620e62529280daa4248e537 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Tue, 3 Dec 2024 18:35:19 +0100 Subject: replace pysimplegui with native tkinter implementation --- quiz.py | 58 ++++++++++++++++++++++++++-------------------------------- 1 file changed, 26 insertions(+), 32 deletions(-) (limited to 'quiz.py') diff --git a/quiz.py b/quiz.py index 616ab79..506482d 100755 --- a/quiz.py +++ b/quiz.py @@ -19,12 +19,12 @@ from pathlib import Path import random import platform import os +import tkinter import tomllib from typing import NoReturn import subprocess import sys -import PySimpleGUI as sg import psutil from playsound3 import playsound @@ -34,8 +34,6 @@ CORRECT_SOUND = Path(__file__).parent / 'Correct.mp3' SHOW_QUESTION_CMD = 'xdg-open' -sg.SetOptions(window_location=(0, -200)) - def err(msg: str, status=1) -> NoReturn: """Print the error message to stderr and exit with status""" @@ -159,42 +157,38 @@ def show_question(question: Path): subprocess.Popen(cmd) -def color_button(layout, text: str, color: str): - """Colorize the button containing the text""" - for button in layout: - if button.get_text() == text: - button.update(button_color=color) - break - +def gen_button_onclick(root, button, solution): + """Create a button's onclick method""" -def prompt_solution(solution: str, prompts=None): - """prompt for the solution""" - if prompts is None: - prompts = ['A', 'B', 'C', 'D', 'E'] + if button["text"] == solution: - layout = [[sg.Button(c) for c in prompts]] - window = sg.Window('', layout) + def onclick(): + button["bg"] = '#00FF00' + playsound(str(CORRECT_SOUND)) + for c in button.master.children.values(): + c["command"] = root.destroy + else: - # Event Loop to process "events" and get the "values" of the inputs - while True: - try: - event, _ = window.read() - except KeyboardInterrupt: - sys.exit(0) + def onclick(): + button["bg"] = '#FF0000' + playsound(str(WRONG_SOUND)) - if event == sg.WIN_CLOSED: # if user closes window or clicks cancel - break - if event == solution: - break + return onclick - color_button(layout[0], event, '#FF0000') - playsound(str(WRONG_SOUND)) - color_button(layout[0], event, '#00FF00') - playsound(str(CORRECT_SOUND)) +def prompt_solution(solution: str, prompts=None): + """prompt for the solution""" + if prompts is None: + prompts = ['A', 'B', 'C', 'D', 'E'] - window.read() - window.close() + root = tkinter.Tk() + for i, c in enumerate(prompts): + btn = tkinter.Button(text=c) + btn["command"] = gen_button_onclick(root, btn, solution) + btn.grid(column=i, row=0) + root.title('') + root.attributes('-type', 'dialog') + root.mainloop() def main(): -- cgit v1.2.3