Guest User

Untitled

a guest
Nov 15th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import *
  3. from tkinter import ttk
  4. import tkinter.messagebox as tm
  5. import os
  6. import pickle
  7.  
  8.  
  9. class Quiz (tk.Tk):
  10. def __init__ (self, *args , **kwargs):
  11.  
  12.  
  13. tk.Tk.__init__(self, *args , **kwargs)
  14. tk.Tk.wm_title(self, "Quiz")
  15.  
  16. container = tk.Frame (self)
  17. container.pack (side = "top" , fill = "both" , expand = True)
  18. container.grid_rowconfigure (0,weight = 1)
  19. container.grid_columnconfigure (0,weight = 1)
  20.  
  21. self.frames = {}
  22. for F in (StartPage, Menu, Difficulty, QuestionsStart):
  23.  
  24. frame = F(container, self)
  25. self.frames[F] = frame
  26. frame.grid (row = 0, column = 0 , sticky = "nsew")
  27.  
  28. self.show_frame(StartPage)
  29.  
  30. def show_frame(self,cont):
  31.  
  32. frame = self.frames[cont]
  33. frame.tkraise()
  34.  
  35. class StartPage(tk.Frame):
  36.  
  37. def __init__(self, parent, controller):
  38.  
  39.  
  40. tk.Frame.__init__(self,parent)
  41. label = tk.Label(self, text = "Login")
  42. label.pack(pady = 10 , padx = 10)
  43.  
  44. global Username
  45. User = tk.Label (self, text = "Username")
  46. User.pack()
  47.  
  48. Username = tk.Entry(self)
  49. Username.pack()
  50. Pass = tk.Label (self, text = "Password")
  51. Pass.pack()
  52.  
  53. Password = tk.Entry (self, show = "*")
  54. Password.pack()
  55.  
  56. button1 = ttk.Button(self, text = "Login",
  57. command = lambda: Login(Username,Password,parent,controller,self) )
  58. button1.pack()
  59.  
  60. button2 = ttk.Button(self, text = "Sign Up",
  61. command = lambda: Signup())
  62. button2.pack()
  63.  
  64.  
  65.  
  66. class Menu(tk.Frame):
  67.  
  68. def __init__ (self, parent, controller):
  69.  
  70. tk.Frame.__init__(self, parent)
  71. label = tk.Label(self, text = "Menu")
  72. label.pack()
  73.  
  74. Label = tk.Label(self, text = "Please enter Maths or Chemsitry")
  75. Label.pack()
  76.  
  77. Topic = tk.Entry(self)
  78. Topic.pack()
  79.  
  80. Proceed = ttk.Button(self, text = "Proceed", command = lambda: controller.show_frame(Difficulty))
  81. Proceed.pack()
  82.  
  83. Result = ttk.Button(self, text = "Results",
  84. command = lambda: Results(controller))
  85. Result.pack()
  86.  
  87. Logout = ttk.Button(self, text = "Log Out",
  88. command = lambda: controller.show_frame(StartPage))
  89. Logout.pack()
  90.  
  91.  
  92.  
  93. class Difficulty(tk.Frame):
  94.  
  95. def __init__ (self, parent, controller):
  96.  
  97. tk.Frame.__init__(self,parent)
  98. label = tk.Label(self, text = "Difficulty")
  99. label.pack()
  100.  
  101. Label = tk.Label(self, text = "Please enter Easy, Medium or Hard")
  102. Label.pack()
  103.  
  104. DifficultyInput = tk.Entry(self)
  105. DifficultyInput.pack()
  106.  
  107. StartQuiz = tk.Button(self , text = "Start Quiz", command = lambda: controller.show_frame(QuestionsStart))
  108. StartQuiz.pack()
  109.  
  110.  
  111.  
  112. backtomenu = ttk.Button(self, text = "Back to Menu",
  113. command = lambda: controller.show_frame(Menu))
  114. backtomenu.pack()
  115.  
  116.  
  117. def DictFromFile ():
  118. TempDict = {}
  119. try:
  120. with open("Questions.txt" , "r") as file:
  121. TempDict = eval(file.read())
  122. file.close()
  123. except IOError as error:
  124. print (error)
  125. print(TempDict)
  126. return TempDict
  127.  
  128. DictFromFile()
  129.  
  130. def QuizType(Topic, DifficultyInput):
  131.  
  132. QuizSelection = str(Topic.get().lower() + DifficultyInput.get().lower())
  133. return QuizSelection
  134.  
  135. class QuestionsStart (tk.Frame):
  136. def __init__(self , parent, controller):
  137.  
  138. tk.Frame.__init__(self,parent)
  139. Label = tk.Label(self, text = DictFromFile(TempDict[QuizSelection(Topic, DifficultyInput)]["Question1"]))
  140. Label.pack()
  141.  
  142.  
  143.  
  144.  
  145. def Login(Username,Password,parent,controller,self):
  146.  
  147. Usernames = []
  148. Login = True
  149. Username = Username.get()
  150. Password = Password.get()
  151.  
  152. try:
  153.  
  154. with open ("Usernames&Passwords.txt" , "rb" ) as file:
  155. for each in pickle.load(file):
  156. Usernames.append(each.strip("n"))
  157. file.close()
  158.  
  159. except IOError as error:
  160. print (error)
  161.  
  162. for each in range(len(Usernames)):
  163. if Usernames[each] == Username :
  164. if Usernames[each + 1] == Password:
  165. Login = True
  166. controller.show_frame(Menu)
  167. break
  168. else:
  169. Login = False
  170. else:
  171. Login = False
  172. if Login == False:
  173. tm.showinfo("Your Username or Password is incorrect" , "Your Usename or Password is incorrect")
  174.  
  175. def Results (controller):
  176.  
  177. UsersResults = []
  178. Counter = 0
  179. Topic = []
  180. Difficulty = []
  181. Mark = []
  182. Percentage = []
  183. Grade = []
  184.  
  185. try:
  186. with open(Username.get() + ".txt" , "r" , encoding = "UTF-8") as file:
  187. for each in file:
  188. UsersResults.append(each.strip("n"))
  189.  
  190. except IOError as error:
  191. print(error)
  192. print (UsersResults)
  193. print (Counter)
  194. for each in range (len(UsersResults)):
  195. Topic = UsersResults[Counter]
  196. Difficulty = UsersResults[Counter + 1]
  197. Mark = UsersResults[Counter + 2]
  198. Percentage = UsersResults[Counter + 3]
  199. Grade = UsersResults[Counter + 4]
  200. Counter += 5
  201.  
  202.  
  203.  
  204. return UsersResults
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211. app = Quiz()
  212. app.geometry ("500x300")
  213. app.mainloop()
Add Comment
Please, Sign In to add comment