Informatik10/quiz/neuesquiz_app.py

195 lines
6.7 KiB
Python
Raw Normal View History

2022-01-16 20:52:19 +00:00
import tkinter as tk
from tkinter import messagebox as tkm
from tkinter import font as tkfont
DEBUG = True
# Access to Controller
2022-01-16 20:52:19 +00:00
from neuesquiz_controller import *
class Application(tk.Frame):
""" Graphical Application
"""
def __init__(self, master=None):
super().__init__(master)
self.set_fonts_for_tkinter()
# Activate Controller
self.controller = Controller(self)
# Control variable for Radiobuttons
self.selected = tk.StringVar()
#build GUI
self.pack()
self.create_widgets()
def create_widgets(self):
# Anzeige
# Frage
self.lf_q = tk.LabelFrame(self, bg="mintcream")
self.lf_q["text"]="Frage"
self.lf_q.grid(columnspan=2,column=0, row=1, sticky="nwse")
self.namelabel=tk.Label(self.lf_q, bg="mintcream")
self.namelabel["text"] = "Wer war der {}. US-Präsident?".format(self.controller.get_key_of_chosen())
self.namelabel.grid(column=0,row=0)
# Antwort Frame
self.lf_a = tk.LabelFrame(self, bg="ghostwhite", labelanchor="n", width=300)
self.lf_a["text"]="Antwort"
self.lf_a.grid(columnspan=2,column=0, row=2, sticky="nwse")
# Radiobuttons
self.radios = list()
2022-01-16 20:52:19 +00:00
selected_elements = self.controller.current_selection
for count, (el_key, el_value) in enumerate(selected_elements.items()):
r = tk.Radiobutton(self.lf_a, text=el_value,
variable=self.selected, value=el_key,
command=self.controller.select_answer)
r.grid(column=0, row=count, sticky="nw")
self.radios.append(r)
#Button: Check
self.btcheck = tk.Button(self)
self.btcheck["text"] = "Überprüfen"
self.btcheck["command"] = self.controller.check_user_selection
self.btcheck.grid(column=0,row=3)
#Button: Go-on
self.btgoon = tk.Button(self)
self.btgoon["text"]="weiter"
self.btgoon["command"]= self.controller.next_question
self.btgoon["state"]="disabled"
self.btgoon.grid(column=1,row=3)
#Label: Ergebnis
self.reslabel=tk.Label(self)
self.reslabel["text"] = ""
self.reslabel.grid(column=0,row=4)
#Label: Punkte
self.pointslabel=tk.Label(self)
self.pointslabel["text"] = "0 von 0 richtig."
self.pointslabel.grid(column=1,row=4)
#Button: Quit
self.quit = tk.Button(self, text="Beenden", fg="red",
command=root.destroy)
self.quit.grid(column=1,row=5)
def change_state_user_input(self):
self.btgoon["state"]="disabled"
self.btcheck["state"]="normal"
def change_state_examine(self):
self.btcheck["state"]="disabled"
self.btgoon["state"]="normal"
def set_response_correct(self):
self.mark_correct_answer()
self.reslabel["text"]="richtig!"
self.reslabel["fg"]="green"
def set_response_incorrect(self):
self.mark_answer_incorrect
self.mark_correct_answer()
self.reslabel["text"]="falsch"
self.reslabel["fg"]="red"
# TODO: coloring of correct answer in green
def mark_correct_answer(self):
# Was auf grün setzen? -> Element finden
for element in self.radios:
print("Element finden")
print(element.cget('value'))
# Ausführen: auf grün setzen.
element['bg'] = "green"
2022-01-16 20:52:19 +00:00
# TODO: coloring of selected answer in red if wrong
def mark_answer_incorrect(self):
pass
# TODO: reset of all coloring of radiobuttons
def mark_reset(self):
pass
def update_counter(self):
# get numbers from controller
n_correct = self.controller.get_count_correct()
n_all = self.controller.get_count_all()
# set label
self.pointslabel["text"] = str(n_correct) + " von " + str(n_all) + " richtig."
def update_answers(self):
# delete old Radiobuttons
self.purge_radios()
# set new question text
self.namelabel["text"] = "Wer war der {}. US-Präsident?".format(self.controller.get_key_of_chosen())
# find out new selection
selected_elements = self.controller.current_selection
# create new Radiobuttons
for count, (el_key, el_value) in enumerate(selected_elements.items()):
r = tk.Radiobutton(self.lf_a, text=el_value,
variable=self.selected, value=el_key,
command=self.controller.select_answer)
r.grid(column=0, row=count, sticky="nw")
self.radios.append(r)
# reset of selected variable
self.selected.initialize(value="")
def purge_radios(self):
for r in self.radios:
r.grid_forget()
2022-01-16 20:52:19 +00:00
r.destroy()
del(self.radios)
self.radios = list()
2022-01-16 20:52:19 +00:00
# TODO: reset of all elements
def reset(self):
pass
def set_fonts_for_tkinter(self):
# Standardschritfart
self.defaultFont = tkfont.nametofont("TkDefaultFont")
self.defaultFont.configure(family="Helvetica", size=12, weight=tkfont.NORMAL)
# Menu-Schriftart
self.defaultMenuFont = tkfont.nametofont("TkMenuFont")
self.defaultMenuFont.configure(family="Helvetica", size=11, weight=tkfont.NORMAL)
# Schriftart für Eingabefelder / Listboxen …
self.defaultTextFont = tkfont.nametofont("TkTextFont")
self.defaultTextFont.configure(family="Helvetica", size=12, weight=tkfont.NORMAL)
# Schrifart für Monospace
self.defaultFixedFont = tkfont.nametofont("TkFixedFont")
self.defaultFixedFont.configure(family="Courier", size=9, weight=tkfont.NORMAL)
# Schriftart für Spaltenüberschriften
self.defaultHeadingFont = tkfont.nametofont("TkHeadingFont")
self.defaultHeadingFont.configure(family="Courier", size=14, weight=tkfont.BOLD)
# Schriftart für Überschriften (Fenster und Dialoge)
self.defaultCaptionFont = tkfont.nametofont("TkCaptionFont")
self.defaultCaptionFont.configure(family="Courier", size=14, weight=tkfont.BOLD)
# Schriftart für Icon-Beschriftungen
self.defaultIconFont = tkfont.nametofont("TkIconFont")
self.defaultIconFont.configure(family="Helvetica", size=8, weight=tkfont.NORMAL)
# Schriftart für Tooltip-Beschriftungen
self.defaultTooltipFont = tkfont.nametofont("TkTooltipFont")
self.defaultTooltipFont.configure(family="Helvetica", size=8, weight=tkfont.NORMAL)
root = tk.Tk()
app = Application(master=root)
app.mainloop()