GUI Label control

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

GUI Label control

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line creates a label that shows the text Score: 0 in the window root?
A tk.Label(root, 'Score: 0').text
B tk.Label(root, text='Score: 0')
2. A label is stored in a variable called result_label. Which line changes the text it shows to Pass?
A result_label.get('Pass')
B result_label.config(text='Pass')
3. True or false: A Label control is mainly used to show text or output to the user, not to type into.
A True
B False
4. Which line makes a label show its text in red?
A tk.Label(root, text='Error', fg='red')
B tk.Label(root, text='Error', colour='red')
5. Which line sets a label's font to Arial size 16?
A tk.Label(root, text='Hi', font=('Arial', 16))
B tk.Label(root, text='Hi', font='Arial' 16)

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1tk.Label(root, text='Score: 0') AlblOutput.config(text=str(total))
2lblResult.config(text='Total: 25') BChanges the existing label so it now shows Total: 25
3Style a label with a font and a text colour CCreates a label in the window that displays the text Score: 0
4Show a calculated result in a label after a calculation Dtk.Label(root, text='Hi', font=('Arial', 16), fg='blue')
5lblResult = tk.Label(root, 'Hello') EThe text is not given using the text= option, so the label does not know it is the text to show. Use text='Hello'.
6lblResult.config(text=total) lblResult.text = 'Done' FSetting .text directly does not change the label. Use lblResult.config(text='Done') to update what it shows.

Definitions

tk.Label(root, text='...')

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
lbl = tk._____(root, text='Score: 0')
2.
lbl = tk.Label(root, ____='Welcome')
3.
result = 7 * 6 lbl.______(text=result)
4.
lbl = tk.Label(root, text='Hi', __='red')
5.
lbl = tk.Label(root, text='Hi', ____=('Arial', 16))

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A root = tk.Tk()
B lbl = tk.Label(root, text="Hi")
C lbl.pack()
D lbl.config(txt="Bye")
E root.mainloop()
Question 2
A root = tk.Tk()
B title = tk.label(root, text="Score")
C title.pack()
D root.mainloop()
Question 3
A root = tk.Tk()
B result = tk.Label(root, text="")
C result.pack()
D result.config(text="Total: 5"
E root.mainloop()
Question 4
A import Tkinter as tk
B root = tk.Tk()
C lbl = tk.Label(root, text="Hello")
D lbl.pack()
Question 5
A root = tk.Tk()
B lbl = tk.Label(root, text=Hi)
C lbl.config(fg="red")
D lbl.pack()

Answer Key

GUI Label control
Section A: Quick Questions
Q1
B — tk.Label(root, text='Score: 0')
Q2
B — result_label.config(text='Pass')
Q3
A — True
Q4
A — tk.Label(root, text='Error', fg='red')
Q5
A — tk.Label(root, text='Hi', font=('Arial', 16))
Section B: Match the Pairs
Q1
tk.Label(root, text='Score: 0') → Creates a label in the window that displays the text Score: 0
Q2
lblResult.config(text='Total: 25') → Changes the existing label so it now shows Total: 25
Q3
Style a label with a font and a text colour → tk.Label(root, text='Hi', font=('Arial', 16), fg='blue')
Q4
Show a calculated result in a label after a calculation → lblOutput.config(text=str(total))
Q5
lblResult = tk.Label(root, 'Hello') → The text is not given using the text= option, so the label does not know it is the text to show. Use text='Hello'.
Q6
lblResult.config(text=total) lblResult.text = 'Done' → Setting .text directly does not change the label. Use lblResult.config(text='Done') to update what it shows.
Section C: Definitions
Q1
tk.Label(root, text='...') — Creates a tkinter label that shows text in the window. Use .config(text=...) to change what it shows, such as displaying a result. Options like font and fg set style.
Section D: Fill in the Blanks
Q1
lbl = tk.Label(root, text='Score: 0')
Q2
lbl = tk.Label(root, text='Welcome')
Q3
result = 7 * 6 lbl.config(text=result)
Q4
lbl = tk.Label(root, text='Hi', fg='red')
Q5
lbl = tk.Label(root, text='Hi', font=('Arial', 16))
Section E: Spot the Error
Q1
D — The option that changes a label's words is text, not txt. It should be lbl.config(text="Bye").
Q2
B — The class name is case-sensitive. It must be tk.Label with a capital L, not tk.label.
Q3
D — The line is missing its closing bracket. It should be result.config(text="Total: 5").
Q4
A — In Python 3 the module name is all lowercase. It should be import tkinter as tk.
Q5
B — The label text is a string, so it needs quotes. Without them Python looks for a variable called Hi. It should be text="Hi".