Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # http://stackoverflow.com/questions/34658585/python-tkinter-gui
- from tkinter import *
- import random
- # -- functions ---
- def sel():
- selection = "You selected the option " + str(var.get())
- message.set(selection)
- def submit_btn ():
- global score
- btn_submit.config(state="disabled")
- if var.get() == all_questions[questions_order[current_question]]['correct_answer'] :
- score += 1
- points.set("Your score is: {}".format(str(score)))
- else:
- message.set("Sorry wrong answer")
- # change image
- badge.config(image=photo2)
- # show image (if it was hidden)
- #badge.pack(before=points_label)
- def next_btn():
- global current_question
- current_question += 1
- set_question(current_question)
- # function used in two places - next_btn and to set first question
- def set_question(number):
- if number < len(all_questions):
- # get question number from "random order" list
- number = questions_order[number]
- question.set(all_questions[number]['question'])
- answer1.set(all_questions[number]['answer1'])
- answer2.set(all_questions[number]['answer2'])
- answer3.set(all_questions[number]['answer3'])
- answer4.set(all_questions[number]['answer4'])
- # reset elements
- var.set(0)
- btn_submit.config(state="active")
- #badge.pack_forget() # hide image
- message.set("")
- else:
- #badge.pack_forget() # hide image
- message.set("It was last question")
- def quit_btn():
- root.destroy()
- # --- main ---
- all_questions = [
- {
- 'question': "Why is Pc better than Mac?",
- 'answer1': "Easy to Use",
- 'answer2': "Less Vulnerable to Viruses",
- 'answer3': "Boot time is faster",
- 'answer4': "Stable Operating System",
- 'correct_answer': 1,
- },
- {
- 'question': "Which of the following is a product of Mac?",
- 'answer1': "Alien Ware",
- 'answer2': "ZX-80 microcomputer",
- 'answer3': "POWERBOOK 100",
- 'answer4': "Commodore 64",
- 'correct_answer': 3,
- },
- {
- 'question': "What year was Mactonish introduced?",
- 'answer1': "1984",
- 'answer2': "1989",
- 'answer3': "1993",
- 'answer4': "1995",
- 'correct_answer': 1,
- },
- {
- 'question': "Which category is worst for Mac?",
- 'answer1': "Photo/Video Editing",
- 'answer2': "Maintenance",
- 'answer3': "Design",
- 'answer4': "Gaming",
- 'correct_answer': 4,
- },
- {
- 'question': "Who helped Steve Jobs introduce Macintosh?",
- 'answer1': "Ronald Wayne",
- 'answer2': "Steve Wozniak",
- 'answer3': "Mike Markkula",
- 'answer4': "John Sculley",
- 'correct_answer': 2,
- },
- {
- 'question': "One of the following is not Macintosh 128k specs",
- 'answer1': "Photo/Video Editing",
- 'answer2': "Maintenance",
- 'answer3': "Design",
- 'answer4': "Gaming",
- 'correct_answer': 4,
- }
- ]
- # - init -
- root = Tk()
- # - variables -
- var = IntVar()
- question = StringVar()
- answer1 = StringVar()
- answer2 = StringVar()
- answer3 = StringVar()
- answer4 = StringVar()
- message = StringVar()
- points = StringVar(value="Your score is: 0")
- score = 0
- # - create elements -
- photo1 = PhotoImage(file="badge_1.gif")
- photo2 = PhotoImage(file="badge_2.gif")
- # show first image
- badge = Label(root, image=photo1)
- badge.pack()
- badge.some_variable_1 = photo1 # solution for some problem with Garbage Collector (GC)
- badge.some_variable_2 = photo2 # solution for some problem with Garbage Collector (GC)
- # ---
- points_label = Label(root, textvariable=points, relief=RAISED, width=30)
- points_label.pack(fill='both')
- question_label = Label(root, textvariable=question, relief=RAISED)
- question_label.pack(fill='both')
- # ---
- choice1 = Radiobutton(root, textvariable=answer1, variable=var, value=1, command=sel)
- choice1.pack(anchor='w')
- choice2 = Radiobutton(root, textvariable=answer2, variable=var, value=2, command=sel)
- choice2.pack(anchor='w')
- choice3 = Radiobutton(root, textvariable=answer3, variable=var, value=3, command=sel)
- choice3.pack(anchor='w')
- choice4 = Radiobutton(root, textvariable=answer4, variable=var, value=4, command=sel)
- choice4.pack(anchor='w')
- # ---
- message_label = Label(root, textvariable=message, relief=RAISED)
- message_label.pack(fill='both')
- # ---
- btn_submit = Button(root, text="Submit", fg="red", command=submit_btn)
- btn_submit.pack(fill='both')
- btn_next = Button(root, text="Next", fg="green", command=next_btn)
- btn_next.pack(fill='both')
- btn_quit = Button(root, text="Quit", fg="blue", command=quit_btn)
- btn_quit.pack(fill='both')
- # - set question and answers -
- # questions numbers in random order
- questions_count = len(all_questions)
- questions_order = list(range(questions_count))
- random.shuffle(questions_order)
- # set first question
- current_question = 0
- set_question(current_question)
- # - start the engine -
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement