Informatik10/quiz/neuesquiz.py

132 lines
3.4 KiB
Python
Raw Normal View History

2022-01-16 16:46:21 +00:00
import tkinter as tk
from random import randint
D = {1: "George Washington", 2: "John Adams", 3: "Thomas Jefferson",
4: "James Madison", 5: "James Monroe"}
P = list(D.values())
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.sel=tk.StringVar()
self.chosen=randint(0,len(D)-1)
self.cntCorr=0
self.cntWrong=0
#build GUI
self.pack()
self.create_widgets()
print(self.chosen)
def create_widgets(self):
#Anzeige
# Frage
self.lf_q = tk.LabelFrame(self)
self.lf_q["text"]="Frage"
self.lf_q.grid(columnspan=2,column=0, row=1)
self.namelabel=tk.Label(self.lf_q)
self.namelabel["text"] = "Wer war der " + str(list(D.keys())[self.chosen]) + ". US-Präsident?"
self.namelabel.grid(column=0,row=0)
# Antwort
self.lf_a = tk.LabelFrame(self)
self.lf_a["text"]="Antwort"
self.lf_a.grid(columnspan=2,column=0, row=2)
# Antwort 1
self.l_a1 = tk.Radiobutton(self.lf_a, variable=self.sel, command=self.selectParty)
self.l_a1["text"]="Test1"
self.l_a1.grid(column=0,row=0)
# Antwort 2
#self.l_a2 = tk.Radiobutton(self.lf_a, variable=self.sel, command=self.selectParty)
#self.l_a2["text"]="Test2"
#self.l_a2.grid(column=0,row=1)
# Antwort 3
self.l_a3 = tk.Radiobutton(self.lf_a, variable=self.sel, command=self.selectParty)
self.l_a3["text"]="Test3"
self.l_a3.grid(column=0,row=2)
self.radios=list()
for party in P:
r = tk.Radiobutton(self.lf, text=party, variable=self.sel, value = party, command=self.selectParty)
r.pack(anchor="w")
self.radios.append(r)
#Button: Check
self.btcheck = tk.Button(self)
self.btcheck["text"] = "Überprüfen"
self.btcheck["command"] = self.checkinput
self.btcheck.grid(column=0,row=2)
#Button: Go-on
self.btgoon = tk.Button(self)
self.btgoon["text"]="weiter"
self.btgoon["command"]= self.nextq
self.btgoon["state"]="disabled"
self.btgoon.grid(column=1,row=2)
#Label: Ergebnis
self.reslabel=tk.Label(self)
self.reslabel["text"] = ""
self.reslabel.grid(column=0,row=3)
#Label: Punkte
self.pointslabel=tk.Label(self)
self.pointslabel["text"] = str(self.cntCorr) + " von " + str(self.cntCorr+self.cntWrong) + " richtig."
self.pointslabel.grid(column=1,row=3)
#Button: Quit
self.quit = tk.Button(self, text="QUIT", fg="red",
command=root.destroy)
self.quit.grid(column=1,row=4)
def selectParty(self):
selection=self.sel.get()
print("Partei ausgewählt: " + selection)
def checkinput(self):
self.btcheck["state"]="disabled"
self.btgoon["state"]="normal"
if self.sel.get() == D[list(D.keys())[self.chosen]]:
self.reslabel["text"]="richtig!"
self.reslabel["fg"]="green"
self.cntCorr+=1
self.pointslabel["text"] = str(self.cntCorr) + " von " + str(self.cntCorr+self.cntWrong) + " richtig."
else:
self.reslabel["text"]="falsch."
self.reslabel["fg"]="red"
self.cntWrong+=1
self.pointslabel["text"] = str(self.cntCorr) + " von " + str(self.cntCorr+self.cntWrong) + " richtig."
def nextq(self):
self.btgoon["state"]="disabled"
self.btcheck["state"]="normal"
for radio in self.radios:
radio.deselect()
self.chosen=randint(0,len(D)-1)
self.namelabel["text"] = "Politiker: " + list(D.keys())[self.chosen]
self.reslabel["text"] = ""
def win(self):
print("Gewonnen!")
root = tk.Tk()
app = Application(master=root)
app.mainloop()