ich brauche mal einen Arschtritt, weil ich nicht drauf komme wie ich sinnvoll, die Wiederholung der Funktion eliminiere.
Code: Alles auswählen
def control1(self):
if not self.lock:
if self.right_ans != self.a1:
self.answer_button1.configure(bg="red")
else:
self.right += 1
self.right_ans_button.configure(bg="green")
self.lock = True
Die Funktion soll das Ergebnis kontrollieren. Leider funktioniert das nur korrekt, wenn ich die Funktion in 4 verschiedenen Ausführungen
implementiere. Das geht mit Sicherheit besser und eleganter. Kann wer was dazu sagen?
Danke
hier nochmal der ganze code zum besseren Verständnis
Code: Alles auswählen
from tkinter import Button, Label, Tk, Text, END
import random
root = Tk()
root.title("Quizzz")
root.geometry("800x700")
questions = [["Wessen Mutter schwitzt nicht beim kacken?", "Merkels Mom", "Merkel selbst", "Söders Mom", "Chuck Norris Mom"],
["Wer oder was ist ein 'Spahn?", "Patient Zero", "Füllmaterial für Hamsterkäfig", "berühmte Trümmerfrau", " aktueller Gesundheitsminister"]]
def clear():
line = root.grid_slaves()
for n in line:
n.destroy()
class Quiz:
def __init__(self,quest):
clear()
self.questions = []
for n in quest:
self.questions.append(n)
self.a1 = ""
self.a2 = ""
self.a3 = ""
self.a4 = ""
self.right_ans = ""
self.right_ans_button = Button(root, text="", font=("Arial", 14))
self.answer_button1 = Button(root, text="", font=("Arial", 14))
self.answer_button2 = Button(root, text="", font=("Arial", 14))
self.answer_button3 = Button(root, text="", font=("Arial", 14))
self.answer_button4 = Button(root, text="", font=("Arial", 14))
self.next = Button(root, text="nächste Frage", font=("Arial", 14), command=self.question)
self.lock_button = Button(root, text="bestätigen", font=("Arial", 14), command=self.lock_it)
self.lock = False
self.right = 0
self.number = 0
self.Max = 2 # input button erstellen
self.question()
def lock_it(self):
pass # lock funktion schreiben
def question(self):
self.next.grid(column=0, row=6, pady=5)
self.lock_button.grid(column=0, row=5, pady=5)
if len(self.questions) > 0 and self.number < self.Max:
self.number += 1
self.lock = False
rand_num = random.randint(0, len(self.questions) - 1)
quest_line = self.questions[rand_num][0]
self.right_ans = self.questions[rand_num][-1]
answers = []
for i in range(1,5):
answers.append(self.questions[rand_num][i])
random.shuffle(answers)
self.a1 = answers[0]
self.a2 = answers[1]
self.a3 = answers[2]
self.a4 = answers[3]
the_question = Text(root, font=("Arial", 14), width=40, height=2)
the_question.insert(END, quest_line)
the_question.grid(column=0, row=0, padx=80, pady=(75, 0))
self.answer_button1 = Button(root, text=self.a1, font=("Arial", 14), width=39, command = self.control1)
self.answer_button2 = Button(root, text=self.a2, font=("Arial", 14), width=39, command = self.control2)
self.answer_button3 = Button(root, text=self.a3, font=("Arial", 14), width=39, command = self.control3)
self.answer_button4 = Button(root, text=self.a4, font=("Arial", 14), width=39, command = self.control4)
self.answer_button1.grid(column=0, row=1, pady=5)
self.answer_button2.grid(column=0, row=2, pady=5)
self.answer_button3.grid(column=0, row=3, pady=5)
self.answer_button4.grid(column=0, row=4, pady=5)
if self.a1 == self.right_ans:
self.right_ans_button = self.answer_button1
elif self.a2 == self.right_ans:
self.right_ans_button = self.answer_button2
elif self.a3 == self.right_ans:
self.right_ans_button = self.answer_button3
elif self.a4 == self.right_ans:
self.right_ans_button = self.answer_button4
self.questions.pop(rand_num)
else:
clear()
lb = Label(root, text="Du hast " + str(self.right) + " von " + str(self.Max) + " Fragen richtig beantwortet!", font=("Arial", 14))
lb.grid(column=0,row=0,padx=120,pady=(170,15))
to_menu = Button(root, text="zurück zum Start", font=("Arial", 14), command=create_menu)
to_menu.grid(column=0,row=1)
def control1(self):
if not self.lock:
if self.right_ans != self.a1:
self.answer_button1.configure(bg="red")
else:
self.right += 1
self.right_ans_button.configure(bg="green")
self.lock = True
def control2(self):
if not self.lock:
if self.right_ans != self.a2:
self.answer_button2.configure(bg="red")
else:
self.right += 1
self.right_ans_button.configure(bg="green")
self.lock = True
def control3(self):
if not self.lock:
if self.right_ans != self.a3:
self.answer_button3.configure(bg="red")
else:
self.right += 1
self.right_ans_button.configure(bg="green")
self.lock = True
def control4(self):
if not self.lock:
if self.right_ans != self.a4:
self.answer_button4.configure(bg="red")
else:
self.right += 1
self.right_ans_button.configure(bg="green")
self.lock = True
class Menu:
def __init__(self):
clear()
self.Quiz = Button(root, text="das Quiz beginnen", font=("Arial", 14), command=create_quiz, width=15, height=3)
self.Quiz.grid(column=0,row=0,padx=275,pady=200)
def create_menu():
return Menu()
def create_quiz():
return Quiz(questions)
create_menu()
root.mainloop()