Advertisement
Guest User

Quiz in Tkinter

a guest
Feb 11th, 2021
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.82 KB | None | 0 0
  1. import tkinter as tk
  2. import random
  3. def show_frame(frame):
  4. frame.tkraise()
  5.  
  6. def validateReg(username, password):
  7. userstr = username.get()
  8. passstr = password.get()
  9. print("Username entered :", userstr)
  10. print("Password entered :", passstr)
  11. text_file = open("passdoc.txt", "w")
  12. text_file.write(userstr+"\n")
  13. text_file.write(passstr+"\n")
  14. text_file.close()
  15.  
  16. def close():
  17. root.destroy()
  18.  
  19. def FindLogin(username, password):
  20. userstr = username.get()
  21. passstr = password.get()
  22. text_file = open("passdoc.txt", "r")
  23. login_info = text_file.read()
  24. if login_info == username and password:
  25. commad=close
  26. text_file.close()
  27.  
  28. def show_question(question):
  29. global current_question
  30. current_question = question
  31.  
  32. # configure the question label to display the question
  33. question_label.configure(text=question.question)
  34.  
  35. # configure the radiobuttons to hold the answers
  36. answers = list(question.answers)
  37. random.shuffle(answers)
  38. for i in range(4):
  39. answer_radiobuttons[i].configure(text=answers[i])
  40.  
  41. answer_var.set(-1) # causes buttons to show as unselected
  42.  
  43. class Question():
  44. def __init__(self, question_string):
  45. subject, question,answer1,answer2,answer3,answer4 = question_string.split(":")
  46. self.subject = subject
  47. self.question = question
  48. self.correct_answer = answer1
  49. self.answers = (answer1, answer2, answer3, answer4)
  50.  
  51. def check_answer():
  52. choice = answer_var.get()
  53. choice = int(choice)
  54. answer = answer_radiobuttons[choice].cget("text")
  55.  
  56. if answer == current_question.correct_answer:
  57. result_label.configure(text="Correct")
  58. else:
  59. result_label.configure(text="Incorrect")
  60.  
  61.  
  62.  
  63. window = tk.Tk()
  64. window.state('zoomed')
  65.  
  66. window.rowconfigure(0,weight=1)
  67. window.columnconfigure(0, weight=1)
  68.  
  69. register = tk.Frame(window)
  70. login = tk.Frame(window)
  71. subject = tk.Frame(window)
  72. english = tk.Frame(window)
  73. maths = tk.Frame(window)
  74. science = tk.Frame(window)
  75. computing = tk.Frame(window)
  76. history = tk.Frame(window)
  77. english_multiple = tk.Frame(window)
  78. english_fill = tk.Frame(window)
  79. english_quick = tk.Frame(window)
  80. english_spag = tk.Frame(window)
  81. difficulty = tk.Frame(window)
  82. diff_ks2 = tk.Frame(window)
  83. diff_ks3 = tk.Frame(window)
  84. diff_ks4 = tk.Frame(window)
  85. diff_adult = tk.Frame(window)
  86.  
  87. game = tk.Frame(window)
  88. question_frame = tk.Frame(game)
  89. game2 = tk.Frame(window)
  90.  
  91.  
  92. for frame in (game,register,login,subject,english,maths,science,computing,history,difficulty,diff_ks2,diff_ks3,diff_ks4,diff_adult,english_multiple,english_fill,english_quick,english_spag,):
  93. frame.grid(row=0,column=0,sticky='nsew')
  94.  
  95.  
  96. #================Register frame
  97. register.configure(background='RoyalBlue4')
  98. register_title = tk.Label(register, text='Register', fg='snow', bg='RoyalBlue4', font=('Chalkboard', 25))
  99. register_title.pack(fill='x')
  100. lblInst = tk.Label(register, text='Please choose username and password:', fg='snow', bg='RoyalBlue4', font=('Chalkboard SE', 25))
  101. lbl = tk.Label(register, text="Username:", fg='snow', bg='RoyalBlue4', font=('Chalkboard'))
  102. ent = tk.Entry(register)
  103. lbl2 = tk.Label(register, text='Password:', fg='snow', bg='RoyalBlue4', font=('Chalkboard'))
  104. ent2 = tk.Entry(register,show='*')
  105. lblInst.pack()
  106. lbl.pack()
  107. ent.pack()
  108. lbl2.pack()
  109. ent2.pack()
  110. register_btn = tk.Button(register, text='Next ->', font=('Chalkboard'), command=lambda:show_frame(login))
  111. register_btn.pack()
  112.  
  113. #================Login frame
  114. login.configure(background='RoyalBlue4')
  115. login_title = tk.Label(login, text='Login', fg='snow', bg='RoyalBlue4', font=('Chalkboard', 25))
  116. login_title.pack(fill='x')
  117. lblInst = tk.Label(login, text='Please enter username and password:', fg='snow', bg='RoyalBlue4', font=('Chalkboard SE', 25))
  118. lbl = tk.Label(login, text="Username:", fg='snow', bg='RoyalBlue4', font=('Chalkboard'))
  119. ent = tk.Entry(login)
  120. lbl2 = tk.Label(login, text='Password:', fg='snow', bg='RoyalBlue4', font=('Chalkboard'))
  121. ent2 = tk.Entry(login,show='*')
  122. lblInst.pack()
  123. lbl.pack()
  124. ent.pack()
  125. lbl2.pack()
  126. ent2.pack()
  127. login_btn = tk.Button(login, text='Next ->', font=('Chalkboard'), command=lambda:show_frame(subject))
  128. login_btn2 = tk.Button(login, text='<- Back', font=('Chalkboard'), command=lambda:show_frame(register))
  129. login_btn.pack()
  130. login_btn2.pack()
  131.  
  132.  
  133. #================Subject frame
  134. subject.configure(background='RoyalBlue4')
  135. subject_title = tk.Label(subject, text='Subject selection', fg='snow', bg='RoyalBlue4', font=('Chalkboard', 25))
  136. subject_title.pack(fill='x')
  137. lblInst = tk.Label(subject, text='Please select a subject:', fg='snow', bg='RoyalBlue4', font=('Chalkboard SE', 25))
  138. lblInst.pack()
  139. lbl.pack()
  140. fr = tk.Frame(subject, height=700, width=1200, bg='RoyalBlue4', borderwidth=30)
  141. fr.place(relx=.5, rely=.4, anchor="c")
  142. btnE = tk.Button(fr, text='English', command=lambda:show_frame(english), fg='gray4', bg='snow', height=2, width=30, font=('Chalkboard'))
  143. btnM = tk.Button(fr, text='Maths', command=lambda:show_frame(maths), fg='gray4', bg='snow', height=2, width=30, font=('Chalkboard'))
  144. btnS = tk.Button(fr, text='Science', command=lambda:show_frame(science), fg='gray4', bg='snow', height=2, width=30, font=('Chalkboard'))
  145. btnC = tk.Button(fr, text='Computing', command=lambda:show_frame(computing), fg='gray4', bg='snow', height=2, width=30, font=('Chalkboard'))
  146. btnH = tk.Button(fr, text='History', command=lambda:show_frame(history), fg='gray4', bg='snow', height=2, width=30, font=('Chalkboard'))
  147.  
  148. btnE.pack()
  149. btnM.pack()
  150. btnS.pack()
  151. btnC.pack()
  152. btnH.pack()
  153.  
  154. subject_button = tk.Button(subject, text='<- Back', font=('Chalkboard'), command=lambda:show_frame(login))
  155. subject_button.pack()
  156.  
  157.  
  158. #================English game type frame
  159. english.configure(background='RoyalBlue4')
  160. english_title = tk.Label(english, text='English game type selection', fg='snow', bg='RoyalBlue4', font=('Chalkboard', 25))
  161. english_title.pack(fill='x')
  162. lblInst = tk.Label(english, text='Please select a game type:', fg='snow', bg='RoyalBlue4', font=('Chalkboard SE', 25))
  163. lblInst.pack()
  164. lbl.pack()
  165. fr = tk.Frame(english, height=700, width=1200, bg='RoyalBlue4', borderwidth=30)
  166. fr.place(relx=.5, rely=.4, anchor="c")
  167. btnMC = tk.Button(fr, text='Multiple choice', command=lambda:show_frame(difficulty), fg='gray4', bg='snow', height=2, width=30, font=('Chalkboard'))
  168. btnMC.pack()
  169. btnFitB = tk.Button(fr, text='Fill in the blank', command=lambda:show_frame(difficulty), fg='gray4', bg='snow', height=2, width=30, font=('Chalkboard'))
  170. btnFitB.pack()
  171. btnQf= tk.Button(fr, text='Quick-fire questions', command=lambda:show_frame(difficulty), fg='gray4', bg='snow', height=2, width=30, font=('Chalkboard'))
  172. btnQf.pack()
  173. btnSPaG = tk.Button(fr, text='SPaG mode', command=lambda:show_frame(difficulty), fg='gray4', bg='snow', height=2, width=30, font=('Chalkboard'))
  174. btnSPaG.pack()
  175. english_button = tk.Button(english, text='<- Back', font=('Chalkboard'), command=lambda:show_frame(subject))
  176. english_button.pack()
  177.  
  178. #===============Difficulty selection
  179. difficulty.configure(background='RoyalBlue4')
  180. difficulty_title = tk.Label(difficulty, text='Difficulty selection', fg='snow', bg='RoyalBlue4', font=('Chalkboard', 25))
  181. difficulty_title.pack(fill='x')
  182. lblInst = tk.Label(difficulty, text='Please select a difficulty:', fg='snow', bg='RoyalBlue4', font=('Chalkboard SE', 25))
  183. lblInst.pack()
  184. lbl.pack()
  185. fr = tk.Frame(difficulty, height=700, width=1200, bg='RoyalBlue4', borderwidth=30)
  186. fr.place(relx=.5, rely=.4, anchor="c")
  187. btn2 = tk.Button(fr, text='KS3', command=lambda:show_frame(game), fg='gray4', bg='snow', height=2, width=30, font=('Chalkboard'))
  188. btn2.pack()
  189. btn3 = tk.Button(fr, text='KS4', command=lambda:show_frame(game), fg='gray4', bg='snow', height=2, width=30, font=('Chalkboard'))
  190. btn3.pack()
  191. btn4 = tk.Button(fr, text='KS5', command=lambda:show_frame(game), fg='gray4', bg='snow', height=2, width=30, font=('Chalkboard'))
  192. btn4.pack()
  193. btnA = tk.Button(fr, text='Adult', command=lambda:show_frame(game), fg='gray4', bg='snow', height=2, width=30, font=('Chalkboard'))
  194. btnA.pack()
  195. difficulty_button = tk.Button(difficulty, text='<- Back', font=('Chalkboard'), command=lambda:show_frame(english))
  196. difficulty_button.pack()
  197.  
  198.  
  199. #==============Game Frame
  200. game.configure(background='RoyalBlue4')
  201. game_title = tk.Label(game, text='Multiple Choice!', fg='snow', bg='RoyalBlue4',font=('Chalkboard', 25))
  202. game_title.pack(fill='x')
  203. question_frame.configure(background='White')
  204. questions = {"English": [], "Maths": [], "Science": []}
  205. lblInst = tk.Label(game, text='Question 1:', fg='snow', bg='RoyalBlue4', font=('Chalkboard SE', 25))
  206. lblInst.pack()
  207. question_label = tk.Label(question_frame, width=80, anchor="c")
  208.  
  209.  
  210. answer_var = tk.StringVar()
  211.  
  212. answer_radiobuttons = (
  213. tk.Radiobutton(question_frame, anchor="w", variable=answer_var, value=0, command=check_answer),
  214. tk.Radiobutton(question_frame, anchor="w", variable=answer_var, value=1, command=check_answer),
  215. tk.Radiobutton(question_frame, anchor="w", variable=answer_var, value=2, command=check_answer),
  216. tk.Radiobutton(question_frame, anchor="w", variable=answer_var, value=3, command=check_answer),
  217. )
  218. result_label = tk.Label(game)
  219.  
  220.  
  221. question_label.pack(side="top", fill="x", anchor="c")
  222. result_label.pack(side="bottom", fill="x")
  223.  
  224. for button in answer_radiobuttons:
  225. button.pack(side="top", fill="x", expand=True, anchor="n")
  226.  
  227. f = open("questions.txt","r")
  228. for row in f.readlines():
  229. question = Question(row)
  230. questions[question.subject].append(question)
  231. show_question(question)
  232. f.close()
  233.  
  234. question_frame.pack()
  235.  
  236. #game_button = tk.Button(game, text='Submit', font=('Chalkboard'), command=lambda:check_answer)
  237. game_button2 = tk.Button(game, text='<- Back', font=('Chalkboard'), command=lambda:show_frame(difficulty))
  238. #game_button.pack()
  239. game_button2.pack()
  240.  
  241. show_frame(register)
  242. window.mainloop()
  243.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement