Advertisement
Guest User

Python tkinter Quiz Teil 2 The Dev

a guest
Jun 16th, 2019
2,529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.43 KB | None | 0 0
  1. import random
  2. from tkinter import *
  3.  
  4. window = Tk()
  5. window.title("Quiz")
  6. window.geometry("600x450")
  7.  
  8. questions = [["Was ist die für den Körper gefährlichste Droge?","LSD","Cannabis","Pilze","Alkohol"]]
  9. questions.append(["Was ist die beste Programmiersprache?","Javascript","C","Delphi","Python"])
  10. questions.append(["Wie kalt ist flüssiger Stickstoff?","-87°C","-236°C","-346°C","-196°C"])
  11. questions.append(["Aus was besteht Licht nicht?","Wellen","Energie","Teilchen","Strom"])
  12. questions.append(["Wer erfand Python?","Albert Einstein","Steve Jobs","James Gosling","Guido van Rossum"])
  13. questions.append(["Wie warm ist die Sonne im Kern?","15000°C","1600°C","200000000°C","15000000°C"])
  14.  
  15. def clear():
  16.     list = window.grid_slaves()
  17.     for n in list:
  18.         n.destroy()
  19.  
  20. class Quiz:
  21.     def __init__(self,quest):
  22.         clear()
  23.         self.Fragen = []
  24.         for n in quest:
  25.             self.Fragen.append(n)
  26.         self.a1=""
  27.         self.a2=""
  28.         self.a3=""
  29.         self.a4=""
  30.         self.Ra=""
  31.         self.RaBtn = Button(window, text="",font=("Arial",14))
  32.         self.antw1 = Button(window, text="",font=("Arial",14))
  33.         self.antw2 = Button(window, text="",font=("Arial",14))
  34.         self.antw3 = Button(window, text="",font=("Arial",14))
  35.         self.antw4 = Button(window, text="",font=("Arial",14))
  36.         self.lock=False
  37.         self.right=0
  38.         self.naechste = Button(window,text="nächste",font=("Arial",14),command=self.Frage)
  39.         self.nummer=0
  40.         self.Max=3
  41.         self.Frage()
  42.     def Frage(self):
  43.         self.naechste.grid(column=0,row=5,pady=5)
  44.         if len(self.Fragen) > 0 and self.nummer < self.Max:
  45.             self.nummer += 1
  46.             self.lock = False
  47.             randNum = random.randint(0,len(self.Fragen)-1)
  48.             fragenText = self.Fragen[randNum][0]
  49.             self.Ra = self.Fragen[randNum][-1]
  50.             answers = []
  51.             for i in range(1,5):
  52.                 answers.append(self.Fragen[randNum][i])
  53.             random.shuffle(answers)
  54.  
  55.             self.a1 = answers[0]
  56.             self.a2 = answers[1]
  57.             self.a3 = answers[2]
  58.             self.a4 = answers[3]
  59.  
  60.             frage = Text(window, font=("Arial", 14), width=40, height=2)
  61.             frage.insert(END,fragenText)
  62.             frage.grid(column=0,row=0,padx=80,pady=(75,0))
  63.  
  64.             self.antw1 = Button(window, text=self.a1, font=("Arial",14),width=39, command = self.control1)
  65.             self.antw2 = Button(window, text=self.a2, font=("Arial",14),width=39, command = self.control2)
  66.             self.antw3 = Button(window, text=self.a3, font=("Arial",14),width=39, command = self.control3)
  67.             self.antw4 = Button(window, text=self.a4, font=("Arial",14),width=39, command = self.control4)
  68.  
  69.             self.antw1.grid(column=0,row=1,pady=(8,5))
  70.             self.antw2.grid(column=0,row=2,pady=5)
  71.             self.antw3.grid(column=0,row=3,pady=5)
  72.             self.antw4.grid(column=0,row=4,pady=5)
  73.  
  74.             if self.a1 == self.Ra:
  75.                 self.RaBtn = self.antw1
  76.             elif self.a2 == self.Ra:
  77.                 self.RaBtn = self.antw2
  78.             elif self.a3 == self.Ra:
  79.                 self.RaBtn = self.antw3
  80.             elif self.a4 == self.Ra:
  81.                 self.RaBtn = self.antw4
  82.             self.Fragen.pop(randNum)
  83.         else:
  84.             clear()
  85.             lb = Label(window, text="Du hast " + str(self.right) + " von " + str(self.Max) + " Fragen richtig beantwortet!",font=("Arial",14))
  86.             lb.grid(column=0,row=0,padx=120,pady=(170,15))
  87.             zumMenu = Button(window, text="Menu",font=("Arial",14),command=menuCreator)
  88.             zumMenu.grid(column=0,row=1)
  89.  
  90.     def control1(self):
  91.         if self.lock == False:
  92.             if self.Ra != self.a1:
  93.                 self.antw1.configure(bg="red")
  94.             else:
  95.                 self.antw1.configure(bg="green")
  96.                 self.right += 1
  97.             self.RaBtn.configure(bg="green")
  98.             self.lock = True
  99.  
  100.     def control2(self):
  101.         if self.lock == False:
  102.             if self.Ra != self.a2:
  103.                 self.antw2.configure(bg="red")
  104.             else:
  105.                 self.antw2.configure(bg="green")
  106.                 self.right += 1
  107.             self.RaBtn.configure(bg="green")
  108.             self.lock = True
  109.  
  110.     def control3(self):
  111.         if self.lock == False:
  112.             if self.Ra != self.a3:
  113.                 self.antw3.configure(bg="red")
  114.             else:
  115.                 self.antw3.configure(bg="green")
  116.                 self.right += 1
  117.             self.RaBtn.configure(bg="green")
  118.             self.lock = True
  119.  
  120.     def control4(self):
  121.         if self.lock == False:
  122.             if self.Ra != self.a4:
  123.                 self.antw4.configure(bg="red")
  124.             else:
  125.                 self.antw4.configure(bg="green")
  126.                 self.right += 1
  127.             self.RaBtn.configure(bg="green")
  128.             self.lock = True
  129.  
  130.  
  131.  
  132. class Menu:
  133.     def __init__(self):
  134.         clear()
  135.         self.Quiz = Button(window, text="Quiz", font=("Arial", 14), command=quizCreator, width=15, height=3)
  136.         self.Quiz.grid(column=0,row=0,padx=218,pady=170)
  137.  
  138. def menuCreator():
  139.     m = Menu()
  140.  
  141. def quizCreator():
  142.     q = Quiz(questions)
  143.  
  144. menuCreator()
  145. window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement