Advertisement
furas

Tkinter - Questions

Jan 7th, 2016
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.98 KB | None | 0 0
  1. # http://stackoverflow.com/questions/34658585/python-tkinter-gui
  2.  
  3.  
  4. from tkinter import *
  5. import random
  6.  
  7. # -- functions ---
  8.  
  9. def sel():
  10.     selection = "You selected the option " + str(var.get())
  11.     message.set(selection)
  12.  
  13.  
  14. def submit_btn ():
  15.     global score
  16.  
  17.     btn_submit.config(state="disabled")
  18.  
  19.     if var.get() == all_questions[questions_order[current_question]]['correct_answer'] :
  20.        score += 1
  21.        points.set("Your score is: {}".format(str(score)))
  22.     else:
  23.        message.set("Sorry wrong answer")
  24.  
  25.     # change image
  26.     badge.config(image=photo2)
  27.    
  28.     # show image (if it was hidden)
  29.     #badge.pack(before=points_label)
  30.    
  31.  
  32. def next_btn():
  33.     global current_question
  34.    
  35.     current_question += 1
  36.     set_question(current_question)
  37.    
  38.    
  39. # function used in two places - next_btn and to set first question
  40. def set_question(number):
  41.  
  42.     if number < len(all_questions):
  43.         # get question number from "random order" list
  44.         number = questions_order[number]
  45.                
  46.         question.set(all_questions[number]['question'])
  47.         answer1.set(all_questions[number]['answer1'])
  48.         answer2.set(all_questions[number]['answer2'])
  49.         answer3.set(all_questions[number]['answer3'])
  50.         answer4.set(all_questions[number]['answer4'])
  51.  
  52.         # reset elements
  53.         var.set(0)
  54.         btn_submit.config(state="active")
  55.         #badge.pack_forget() # hide image
  56.         message.set("")
  57.     else:
  58.         #badge.pack_forget() # hide image
  59.         message.set("It was last question")
  60.  
  61.    
  62.  
  63. def quit_btn():
  64.     root.destroy()
  65.  
  66. # --- main ---
  67.  
  68. all_questions = [
  69.   {
  70.     'question': "Why is Pc better than Mac?",
  71.     'answer1': "Easy to Use",
  72.     'answer2': "Less Vulnerable to Viruses",
  73.     'answer3': "Boot time is faster",
  74.     'answer4': "Stable Operating System",
  75.     'correct_answer': 1,
  76.   },
  77.   {
  78.     'question': "Which of the following is a product of Mac?",
  79.     'answer1': "Alien Ware",
  80.     'answer2': "ZX-80 microcomputer",
  81.     'answer3': "POWERBOOK 100",
  82.     'answer4': "Commodore 64",
  83.     'correct_answer': 3,
  84.   },
  85.   {
  86.     'question': "What year was Mactonish introduced?",
  87.     'answer1': "1984",
  88.     'answer2': "1989",
  89.     'answer3': "1993",
  90.     'answer4': "1995",
  91.     'correct_answer': 1,
  92.   },
  93.   {
  94.     'question': "Which category is worst for Mac?",
  95.     'answer1': "Photo/Video Editing",
  96.     'answer2': "Maintenance",
  97.     'answer3': "Design",
  98.     'answer4': "Gaming",
  99.     'correct_answer': 4,
  100.   },
  101.   {
  102.     'question': "Who helped Steve Jobs introduce Macintosh?",
  103.     'answer1': "Ronald Wayne",
  104.     'answer2': "Steve Wozniak",
  105.     'answer3': "Mike Markkula",
  106.     'answer4': "John Sculley",
  107.     'correct_answer': 2,
  108.   },
  109.   {
  110.     'question': "One of the following is not Macintosh 128k specs",
  111.     'answer1': "Photo/Video Editing",
  112.     'answer2': "Maintenance",
  113.     'answer3': "Design",
  114.     'answer4': "Gaming",
  115.     'correct_answer': 4,
  116.   }
  117. ]
  118.  
  119. # - init -
  120.  
  121. root = Tk()
  122.  
  123. # - variables -
  124.  
  125. var = IntVar()
  126.  
  127. question = StringVar()
  128. answer1 = StringVar()
  129. answer2 = StringVar()
  130. answer3 = StringVar()
  131. answer4 = StringVar()
  132.  
  133. message = StringVar()
  134.  
  135. points = StringVar(value="Your score is: 0")
  136.  
  137. score = 0
  138.  
  139. # - create elements -
  140.  
  141. photo1 = PhotoImage(file="badge_1.gif")
  142. photo2 = PhotoImage(file="badge_2.gif")
  143.  
  144. # show first image
  145. badge = Label(root, image=photo1)
  146. badge.pack()
  147.  
  148. badge.some_variable_1 = photo1 # solution for some problem with Garbage Collector (GC)
  149. badge.some_variable_2 = photo2 # solution for some problem with Garbage Collector (GC)
  150.  
  151. # ---
  152.  
  153. points_label = Label(root, textvariable=points, relief=RAISED, width=30)
  154. points_label.pack(fill='both')
  155.  
  156. question_label = Label(root, textvariable=question, relief=RAISED)
  157. question_label.pack(fill='both')
  158.  
  159. # ---
  160.  
  161. choice1 = Radiobutton(root, textvariable=answer1, variable=var, value=1, command=sel)
  162. choice1.pack(anchor='w')
  163.  
  164. choice2 = Radiobutton(root, textvariable=answer2, variable=var, value=2, command=sel)
  165. choice2.pack(anchor='w')
  166.  
  167. choice3 = Radiobutton(root, textvariable=answer3, variable=var, value=3, command=sel)
  168. choice3.pack(anchor='w')
  169.  
  170. choice4 = Radiobutton(root, textvariable=answer4, variable=var, value=4, command=sel)
  171. choice4.pack(anchor='w')
  172.  
  173. # ---
  174.  
  175. message_label = Label(root, textvariable=message, relief=RAISED)
  176. message_label.pack(fill='both')
  177.  
  178. # ---
  179.  
  180. btn_submit = Button(root, text="Submit", fg="red", command=submit_btn)
  181. btn_submit.pack(fill='both')
  182.  
  183. btn_next = Button(root, text="Next", fg="green", command=next_btn)
  184. btn_next.pack(fill='both')
  185.  
  186. btn_quit = Button(root, text="Quit", fg="blue", command=quit_btn)
  187. btn_quit.pack(fill='both')
  188.  
  189. # - set question and answers -
  190.  
  191. # questions numbers in random order
  192. questions_count = len(all_questions)
  193. questions_order = list(range(questions_count))
  194. random.shuffle(questions_order)
  195. # set first question
  196. current_question = 0
  197. set_question(current_question)
  198.  
  199. # - start the engine -
  200.  
  201. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement