Creating a tkinter window

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

Creating a tkinter window

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line creates the main tkinter window?
A root = tk.Tk()
B root = tk.window()
2. Which import lets you write tk.Tk() in your code?
A import tkinter as tk
B import tk as tkinter
3. Where should root.mainloop() go in a simple tkinter program?
A At the start, before root = tk.Tk()
B At the end, after the window is set up
4. What does root.mainloop() do?
A Repeats the program code ten times
B Shows the window and waits for events like clicks
5. Which line sets the text in the window's title bar to 'My App'?
A root.title('My App')
B root.name('My App')

Match the Pairs

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

Left columnRight column
1root = tk.Tk() ACreates the main tkinter window and stores it in the variable root
2root.mainloop() Broot.title("My App")
3Set the text shown in the title bar of the window Croot.geometry("400x300")
4Set the window size to 400 pixels wide and 300 pixels high Dtk.Tk is missing its brackets, so no window is created. It should be tk.Tk().
5import tkinter as tk root = tk.Tk() root.mainloop() root.title("Quiz") EShows the window and waits for events such as clicks and key presses
6import tkinter as tk root = tk.Tk root.mainloop() Froot.mainloop() is placed before root.title(), so the title line only runs after the window is closed. mainloop() must be the last line.

Definitions

root = tk.Tk()

Fill in the Blanks

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

1.
______ tkinter as tk root = tk.Tk() root.mainloop()
2.
import tkinter as tk root = tk.__() root.mainloop()
3.
import tkinter as tk root = tk.Tk() root.________()
4.
root = tk.Tk() root._____("My App") root.mainloop()
5.
root = tk.Tk() root.________("400x300") root.mainloop()

Spot the Error

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

Question 1
A import tkinter as tk
B root = tk.tk()
C root.title("My App")
D root.mainloop()
Question 2
A import tkinter as tk
B root = tk.Tk()
C root.Title("Quiz")
D root.mainloop()
Question 3
A import tkinter as tk
B root = tk.Tk()
C root.geometry("400x300")
D root.mainloop
Question 4
A import tkinter as tk
B root = tk.Tk()
C root.geometry("400,300")
D root.mainloop()
Question 5
A import Tkinter as tk
B root = tk.Tk()
C root.title("Game")
D root.mainloop()

Answer Key

Creating a tkinter window
Section A: Quick Questions
Q1
A — root = tk.Tk()
Q2
A — import tkinter as tk
Q3
B — At the end, after the window is set up
Q4
B — Shows the window and waits for events like clicks
Q5
A — root.title('My App')
Section B: Match the Pairs
Q1
root = tk.Tk() → Creates the main tkinter window and stores it in the variable root
Q2
root.mainloop() → Shows the window and waits for events such as clicks and key presses
Q3
Set the text shown in the title bar of the window → root.title("My App")
Q4
Set the window size to 400 pixels wide and 300 pixels high → root.geometry("400x300")
Q5
import tkinter as tk root = tk.Tk() root.mainloop() root.title("Quiz") → root.mainloop() is placed before root.title(), so the title line only runs after the window is closed. mainloop() must be the last line.
Q6
import tkinter as tk root = tk.Tk root.mainloop() → tk.Tk is missing its brackets, so no window is created. It should be tk.Tk().
Section C: Definitions
Q1
root = tk.Tk() — Creates the main tkinter window after import tkinter as tk. Call root.mainloop() at the end to show the window and wait for events like clicks.
Section D: Fill in the Blanks
Q1
import tkinter as tk root = tk.Tk() root.mainloop()
Q2
import tkinter as tk root = tk.Tk() root.mainloop()
Q3
import tkinter as tk root = tk.Tk() root.mainloop()
Q4
root = tk.Tk() root.title("My App") root.mainloop()
Q5
root = tk.Tk() root.geometry("400x300") root.mainloop()
Section E: Spot the Error
Q1
B — The window class is Tk with a capital T. tk.tk() causes an AttributeError. It should be root = tk.Tk().
Q2
C — Python is case-sensitive and the method name is all lowercase. root.Title does not exist. It should be root.title("Quiz").
Q3
D — Without brackets, mainloop is not called, so the window closes straight away. It should be root.mainloop().
Q4
C — The size string must be width x height with a lowercase x between the numbers, not a comma. It should be root.geometry("400x300").
Q5
A — In Python 3 the module name is all lowercase. Tkinter with a capital T causes a ModuleNotFoundError. It should be import tkinter as tk.