Python turtle and tkinter
Quiz Master
Make a quiz app with questions in a list, answer buttons, a score and a pop up box at the end.
Today's app asks five questions, one at a time. Click an answer and it tells you straight away whether you were right, adds to your score, and moves on. At the end a pop up box gives your final score. Then you can swap in questions of your own and test your friends.
You will learn
- Keep a whole quiz in a list of lists
- Use one function to show any question
- Check an answer and keep score
- Switch buttons off and show a pop up message
You will need
- A computer with Thonny or IDLE, or this page's Run it here button
- Day 5's project, for labels, buttons and
command
Words to know
- list of lists
- A list whose items are lists, like the rows of a table.
- index
- The position of an item in a list. The first item is number 0.
- len
len(questions)is how many items a list has.- disabled
- A button that is switched off: it is greyed out and cannot be clicked.
Step by step
Step 1: A window with a question
Save a new file as
quiz.py. The question is a Label.wraplength=380wraps long questions onto a new line instead of running off the side. The second import is for the pop up box at the end.1import tkinter as tk2from tkinter import messagebox3 4window = tk.Tk()5window.title("Quiz Master")6window.geometry("420x330")7 8question_label = tk.Label(window, text="Which of these is an input device?",9 font=("Arial", 14), wraplength=380)10question_label.pack(pady=15)11 12window.mainloop()Which of these is an input device?The first question, at the top of the window. Step 2: Three answer buttons
width=24makes every button the same width, 24 letters wide. The buttons do nothing yet.New: lines 12 to 18.
1import tkinter as tk2from tkinter import messagebox3 4window = tk.Tk()5window.title("Quiz Master")6window.geometry("420x330")7 8question_label = tk.Label(window, text="Which of these is an input device?",9 font=("Arial", 14), wraplength=380)10question_label.pack(pady=15)11 12button1 = tk.Button(window, text="Keyboard", width=24)13button1.pack(pady=3)14button2 = tk.Button(window, text="Monitor", width=24)15button2.pack(pady=3)16button3 = tk.Button(window, text="Speaker", width=24)17button3.pack(pady=3)18 19window.mainloop()Which of these is an input device?KeyboardMonitorSpeakerThree answers under the question. Step 3: Put the questions in a list
Each question is a small list of five things: the question, three answers and the right answer.
questionsis a list of those lists.show_questiontakes question numbernumberand puts its parts on the label and buttons.q[0]is the question,q[1]toq[3]the answers.The widgets now start empty, and
show_question()fills them in just before the window opens.New: lines 3 to 20, 26, 29, 31, 33 and 36.
1import tkinter as tk2from tkinter import messagebox3 4questions = [5 ["Which of these is an input device?", "Keyboard", "Monitor", "Speaker", "Keyboard"],6 ["What does CPU stand for?", "Central Processing Unit", "Computer Power Unit",7 "Core Program Utility", "Central Processing Unit"],8 ["How many bits are in a byte?", "4", "8", "16", "8"],9 ["Which language is this quiz written in?", "Scratch", "HTML", "Python", "Python"],10 ["What is the binary number 1010 in denary?", "10", "12", "1010", "10"],11]12 13number = 014 15def show_question():16 q = questions[number]17 question_label.config(text=q[0])18 button1.config(text=q[1])19 button2.config(text=q[2])20 button3.config(text=q[3])21 22window = tk.Tk()23window.title("Quiz Master")24window.geometry("420x330")25 26question_label = tk.Label(window, text="", font=("Arial", 14), wraplength=380)27question_label.pack(pady=15)28 29button1 = tk.Button(window, text="", width=24)30button1.pack(pady=3)31button2 = tk.Button(window, text="", width=24)32button2.pack(pady=3)33button3 = tk.Button(window, text="", width=24)34button3.pack(pady=3)35 36show_question()37window.mainloop()Which of these is an input device?KeyboardMonitorSpeakerThe same first question, now read from the list. Step 4: Check the answer
check(choice)gets the number of the button that was clicked, 1, 2 or 3. If that answer matches the right answer,q[4], the feedback says Correct! in green, and otherwise it shows the right answer in dark red,firebrick. Thennumbergoes up by 1 and the next question appears.A button's
commandcannot pass a number to a function, so each button has a tiny function of its own,choose1,choose2orchoose3, which callscheckwith its number.New: lines 22 to 41, 49, 51, 53 and 55 to 57.
1import tkinter as tk2from tkinter import messagebox3 4questions = [5 ["Which of these is an input device?", "Keyboard", "Monitor", "Speaker", "Keyboard"],6 ["What does CPU stand for?", "Central Processing Unit", "Computer Power Unit",7 "Core Program Utility", "Central Processing Unit"],8 ["How many bits are in a byte?", "4", "8", "16", "8"],9 ["Which language is this quiz written in?", "Scratch", "HTML", "Python", "Python"],10 ["What is the binary number 1010 in denary?", "10", "12", "1010", "10"],11]12 13number = 014 15def show_question():16 q = questions[number]17 question_label.config(text=q[0])18 button1.config(text=q[1])19 button2.config(text=q[2])20 button3.config(text=q[3])21 22def check(choice):23 global number24 right = questions[number][4]25 if questions[number][choice] == right:26 feedback.config(text="Correct!", fg="green")27 else:28 feedback.config(text="The answer was " + right, fg="firebrick")29 number = number + 130 if number < len(questions):31 show_question()32 33def choose1():34 check(1)35 36def choose2():37 check(2)38 39def choose3():40 check(3)41 42window = tk.Tk()43window.title("Quiz Master")44window.geometry("420x330")45 46question_label = tk.Label(window, text="", font=("Arial", 14), wraplength=380)47question_label.pack(pady=15)48 49button1 = tk.Button(window, text="", width=24, command=choose1)50button1.pack(pady=3)51button2 = tk.Button(window, text="", width=24, command=choose2)52button2.pack(pady=3)53button3 = tk.Button(window, text="", width=24, command=choose3)54button3.pack(pady=3)55 56feedback = tk.Label(window, text="", font=("Arial", 12))57feedback.pack(pady=10)58 59show_question()60window.mainloop()What does CPU stand for?Central Processing UnitComputer Power UnitCore Program UtilityCorrect!Keyboard was right, and question 2 is up. Step 5: Keep score
Add a
scorethat goes up by 1 for each right answer, and a label to show it.show_questionupdates the score label every time.New: lines 14, 22, 25, 28 and 62 to 64.
1import tkinter as tk2from tkinter import messagebox3 4questions = [5 ["Which of these is an input device?", "Keyboard", "Monitor", "Speaker", "Keyboard"],6 ["What does CPU stand for?", "Central Processing Unit", "Computer Power Unit",7 "Core Program Utility", "Central Processing Unit"],8 ["How many bits are in a byte?", "4", "8", "16", "8"],9 ["Which language is this quiz written in?", "Scratch", "HTML", "Python", "Python"],10 ["What is the binary number 1010 in denary?", "10", "12", "1010", "10"],11]12 13number = 014score = 015 16def show_question():17 q = questions[number]18 question_label.config(text=q[0])19 button1.config(text=q[1])20 button2.config(text=q[2])21 button3.config(text=q[3])22 score_label.config(text="Score: " + str(score))23 24def check(choice):25 global number, score26 right = questions[number][4]27 if questions[number][choice] == right:28 score = score + 129 feedback.config(text="Correct!", fg="green")30 else:31 feedback.config(text="The answer was " + right, fg="firebrick")32 number = number + 133 if number < len(questions):34 show_question()35 36def choose1():37 check(1)38 39def choose2():40 check(2)41 42def choose3():43 check(3)44 45window = tk.Tk()46window.title("Quiz Master")47window.geometry("420x330")48 49question_label = tk.Label(window, text="", font=("Arial", 14), wraplength=380)50question_label.pack(pady=15)51 52button1 = tk.Button(window, text="", width=24, command=choose1)53button1.pack(pady=3)54button2 = tk.Button(window, text="", width=24, command=choose2)55button2.pack(pady=3)56button3 = tk.Button(window, text="", width=24, command=choose3)57button3.pack(pady=3)58 59feedback = tk.Label(window, text="", font=("Arial", 12))60feedback.pack(pady=10)61 62score_label = tk.Label(window, text="Score: 0", font=("Arial", 12, "bold"))63score_label.pack()64 65show_question()66window.mainloop()Which language is this quiz written in?ScratchHTMLPythonThe answer was 8Score: 2Two right and one wrong: the score is 2. Step 6: The end of the quiz
After the last question,
numberis no longer less thanlen(questions), the number of questions, socheckcallsfinish. It switches the buttons off withstate="disabled"and opens a pop up box with the final score.New: lines 35, 36 and 46 to 53.
1import tkinter as tk2from tkinter import messagebox3 4questions = [5 ["Which of these is an input device?", "Keyboard", "Monitor", "Speaker", "Keyboard"],6 ["What does CPU stand for?", "Central Processing Unit", "Computer Power Unit",7 "Core Program Utility", "Central Processing Unit"],8 ["How many bits are in a byte?", "4", "8", "16", "8"],9 ["Which language is this quiz written in?", "Scratch", "HTML", "Python", "Python"],10 ["What is the binary number 1010 in denary?", "10", "12", "1010", "10"],11]12 13number = 014score = 015 16def show_question():17 q = questions[number]18 question_label.config(text=q[0])19 button1.config(text=q[1])20 button2.config(text=q[2])21 button3.config(text=q[3])22 score_label.config(text="Score: " + str(score))23 24def check(choice):25 global number, score26 right = questions[number][4]27 if questions[number][choice] == right:28 score = score + 129 feedback.config(text="Correct!", fg="green")30 else:31 feedback.config(text="The answer was " + right, fg="firebrick")32 number = number + 133 if number < len(questions):34 show_question()35 else:36 finish()37 38def choose1():39 check(1)40 41def choose2():42 check(2)43 44def choose3():45 check(3)46 47def finish():48 score_label.config(text="Score: " + str(score))49 question_label.config(text="Quiz over!")50 button1.config(state="disabled")51 button2.config(state="disabled")52 button3.config(state="disabled")53 messagebox.showinfo("Quiz Master", "You scored " + str(score) + " out of " + str(len(questions)))54 55window = tk.Tk()56window.title("Quiz Master")57window.geometry("420x330")58 59question_label = tk.Label(window, text="", font=("Arial", 14), wraplength=380)60question_label.pack(pady=15)61 62button1 = tk.Button(window, text="", width=24, command=choose1)63button1.pack(pady=3)64button2 = tk.Button(window, text="", width=24, command=choose2)65button2.pack(pady=3)66button3 = tk.Button(window, text="", width=24, command=choose3)67button3.pack(pady=3)68 69feedback = tk.Label(window, text="", font=("Arial", 12))70feedback.pack(pady=10)71 72score_label = tk.Label(window, text="Score: 0", font=("Arial", 12, "bold"))73score_label.pack()74 75show_question()76window.mainloop()Quiz over!10121010Correct!Score: 4iYou scored 4 out of 5OKThe quiz is over: a pop up box gives the score. Step 7: Add your own question
Now make the quiz yours. Add a question to the list, in the same pattern: the question, three answers and the right answer, all in quotes. The quiz works out how many questions there are by itself.
New: line 11.
1import tkinter as tk2from tkinter import messagebox3 4questions = [5 ["Which of these is an input device?", "Keyboard", "Monitor", "Speaker", "Keyboard"],6 ["What does CPU stand for?", "Central Processing Unit", "Computer Power Unit",7 "Core Program Utility", "Central Processing Unit"],8 ["How many bits are in a byte?", "4", "8", "16", "8"],9 ["Which language is this quiz written in?", "Scratch", "HTML", "Python", "Python"],10 ["What is the binary number 1010 in denary?", "10", "12", "1010", "10"],11 ["Which of these is the fastest to get data from?", "RAM", "A hard disk", "A USB stick", "RAM"],12]13 14number = 015score = 016 17def show_question():18 q = questions[number]19 question_label.config(text=q[0])20 button1.config(text=q[1])21 button2.config(text=q[2])22 button3.config(text=q[3])23 score_label.config(text="Score: " + str(score))24 25def check(choice):26 global number, score27 right = questions[number][4]28 if questions[number][choice] == right:29 score = score + 130 feedback.config(text="Correct!", fg="green")31 else:32 feedback.config(text="The answer was " + right, fg="firebrick")33 number = number + 134 if number < len(questions):35 show_question()36 else:37 finish()38 39def choose1():40 check(1)41 42def choose2():43 check(2)44 45def choose3():46 check(3)47 48def finish():49 score_label.config(text="Score: " + str(score))50 question_label.config(text="Quiz over!")51 button1.config(state="disabled")52 button2.config(state="disabled")53 button3.config(state="disabled")54 messagebox.showinfo("Quiz Master", "You scored " + str(score) + " out of " + str(len(questions)))55 56window = tk.Tk()57window.title("Quiz Master")58window.geometry("420x330")59 60question_label = tk.Label(window, text="", font=("Arial", 14), wraplength=380)61question_label.pack(pady=15)62 63button1 = tk.Button(window, text="", width=24, command=choose1)64button1.pack(pady=3)65button2 = tk.Button(window, text="", width=24, command=choose2)66button2.pack(pady=3)67button3 = tk.Button(window, text="", width=24, command=choose3)68button3.pack(pady=3)69 70feedback = tk.Label(window, text="", font=("Arial", 12))71feedback.pack(pady=10)72 73score_label = tk.Label(window, text="Score: 0", font=("Arial", 12, "bold"))74score_label.pack()75 76show_question()77window.mainloop()Quiz over!RAMA hard diskA USB stickCorrect!Score: 5iYou scored 5 out of 6OKSix questions now, and the pop up box says out of 6. Watch out Every item ends with a comma, except the last in each list. A missing comma or quote is the most common bug today.
Test it
Tick each one when it works. If one does not, look back at the step it comes from.
Stretch challenges
Finished early? Try these. Each one has a hint if you get stuck.
Shuffle
Ask the questions in a different order every time.
Show a hint
import randomat the top, thenrandom.shuffle(questions)just beforeshow_question()at the bottom.Play again
Add a Play again button that starts the quiz from the first question with a score of 0.
Show a hint
Its function sets
numberandscoreback to 0 (bothglobal), switches the answer buttons on again withstate="normal"and callsshow_question().Four answers
Give every question four answers instead of three.
Show a hint
Every question needs one more answer, so the right answer moves to
q[5]. Add abutton4with achoose4function, and set its text inshow_question.
The finished code
Show the whole program
1import tkinter as tk2from tkinter import messagebox3 4questions = [5 ["Which of these is an input device?", "Keyboard", "Monitor", "Speaker", "Keyboard"],6 ["What does CPU stand for?", "Central Processing Unit", "Computer Power Unit",7 "Core Program Utility", "Central Processing Unit"],8 ["How many bits are in a byte?", "4", "8", "16", "8"],9 ["Which language is this quiz written in?", "Scratch", "HTML", "Python", "Python"],10 ["What is the binary number 1010 in denary?", "10", "12", "1010", "10"],11 ["Which of these is the fastest to get data from?", "RAM", "A hard disk", "A USB stick", "RAM"],12]13 14number = 015score = 016 17def show_question():18 q = questions[number]19 question_label.config(text=q[0])20 button1.config(text=q[1])21 button2.config(text=q[2])22 button3.config(text=q[3])23 score_label.config(text="Score: " + str(score))24 25def check(choice):26 global number, score27 right = questions[number][4]28 if questions[number][choice] == right:29 score = score + 130 feedback.config(text="Correct!", fg="green")31 else:32 feedback.config(text="The answer was " + right, fg="firebrick")33 number = number + 134 if number < len(questions):35 show_question()36 else:37 finish()38 39def choose1():40 check(1)41 42def choose2():43 check(2)44 45def choose3():46 check(3)47 48def finish():49 score_label.config(text="Score: " + str(score))50 question_label.config(text="Quiz over!")51 button1.config(state="disabled")52 button2.config(state="disabled")53 button3.config(state="disabled")54 messagebox.showinfo("Quiz Master", "You scored " + str(score) + " out of " + str(len(questions)))55 56window = tk.Tk()57window.title("Quiz Master")58window.geometry("420x330")59 60question_label = tk.Label(window, text="", font=("Arial", 14), wraplength=380)61question_label.pack(pady=15)62 63button1 = tk.Button(window, text="", width=24, command=choose1)64button1.pack(pady=3)65button2 = tk.Button(window, text="", width=24, command=choose2)66button2.pack(pady=3)67button3 = tk.Button(window, text="", width=24, command=choose3)68button3.pack(pady=3)69 70feedback = tk.Label(window, text="", font=("Arial", 12))71feedback.pack(pady=10)72 73score_label = tk.Label(window, text="Score: 0", font=("Arial", 12, "bold"))74score_label.pack()75 76show_question()77window.mainloop()Notes for the teacher
Timing
- 10 minutes: steps 1 and 2.
- 15 minutes: step 3. Lists of lists are new for most students: draw the list as a table on the board, one row a question.
- 20 minutes: steps 4 to 6.
- 10 minutes: step 7, then students swap seats and take each other's quizzes.
Mistakes to look out for
- A missing comma or quote inside the list: SyntaxError, often reported on the line after the real mistake.
- A right answer that does not exactly match one of the three answers, such as
"keyboard"against"Keyboard": the question can never be answered correctly. global number, scoremissing fromcheck: UnboundLocalError.checkdefined after the buttons that use it is fine, butchoose1and the others must be defined before the buttons are made, becausecommand=choose1needs the function to exist already.
Going further
The data (the questions) is kept apart from the code that uses it. Adding a question changes no code at all, which is how real apps are built. A good link to files: a stretch for keen students is to read the questions from a text file.