Python · 5.1
Building Interfaces with tkinter
Name
Class
Date
Mark / 28
Section A
Circle the letter of the correct answer. One mark each.
- 1What does
window.mainloop()do?1- ACreates the window
- BDraws all the widgets
- CCloses the window
- DStarts the event loop and keeps the window open waiting for user actions
- 2Why might a widget not appear on screen?1
- AIt needs a colour set
- BIt was created but never packed, gridded or placed
- CWidgets appear automatically
- DThe window is too small
- 3What is wrong with
tk.Button(window, command=save())?1- AButtons cannot call functions
- BNothing is wrong
- CThe brackets call save immediately instead of when the button is clicked
- Dcommand is not a valid option
- 4Which layout manager is best for a form with labels and text boxes?1
- Agrid
- Bmainloop
- Cplace
- Dpack
- 5What happens if you use both pack and grid in the same container?1
- AAn exception is raised immediately
- BThe program freezes with no error message
- CThe last one used wins
- DBoth work fine
- 6What does
entry.get()return?1- AA float
- BA widget
- CA string
- DAn integer
- 7What is a StringVar used for?1
- ANaming a window
- BConverting text to numbers
- CStoring a string permanently
- DAutomatically updating any widget bound to it when its value changes
- 8How do you pass an argument to a button's command function?1
- AIt is not possible
- Bcommand=add, args=5
- CUse a lambda, for example command=lambda: add(5)
- Dcommand=add(5)
- 9What does
label.config(text="New")do?1- AChanges the label's text after it has been created
- BCreates a new label
- CMoves the label
- DDeletes the label
- 10Which line creates the main application window?1
- Awindow = tk.Label()
- Bwindow = tk.Tk()
- Cwindow = tk.Frame()
- Dwindow = tk.Window()
Section B
Answer in the space provided. The number of marks tells you how many separate points to make.
- 1ExplainExplain the purpose of the mainloop method in a tkinter program.3
- 2ExplainA student creates a label with
label = tk.Label(window, text="Hello")but nothing appears when the program runs. Explain why and give the correction.2 - 3ExplainExplain the difference between the pack and grid layout managers, and state when each is most suitable.4
- 4WriteWrite a tkinter program that displays a text box and a button, and when the button is clicked shows the entered text in a label.6
- 5ExplainExplain why input taken from a tkinter Entry widget should be validated before it is used.3
Mark scheme
Section A
Section B
1 Explain the purpose of the mainloop method in a tkinter program. 3
One mark for each of the following, to a maximum of 3.
- It starts the event loop
- It keeps the window open and waits for user actions such as clicks and key presses
- Code after it does not run until the window closes
Model answer. The mainloop method starts tkinter's event loop, which is what makes the interface interactive. It keeps the window on screen and continuously waits for events such as button clicks, key presses and mouse movements, dispatching each one to whatever function has been bound to it. Because the program stays inside mainloop until the window is closed, any code written after the mainloop call will not run while the application is open, which is a common source of confusion for beginners.
2 A student creates a label with label = tk.Label(window, text="Hello") but nothing appears when the program runs. Explain why and give the correction. 2
One mark for each of the following, to a maximum of 2.
- The widget has not been added to the window with a layout manager
- Add label.pack() or label.grid() to display it
Model answer. Creating a widget builds it in memory but does not put it on screen. A layout manager must be called to add it to the window, so the student needs to add a line such as label.pack() or label.grid(row=0, column=0) after creating the label. Without this the label exists but is never displayed.
3 Explain the difference between the pack and grid layout managers, and state when each is most suitable. 4
One mark for each of the following, to a maximum of 4.
- pack stacks widgets in the order they are added
- grid places widgets in rows and columns
- pack suits simple stacked layouts
- grid suits forms where labels and inputs must line up
Model answer. The pack layout manager stacks widgets one after another in the order they are added, using a side such as top, bottom, left or right. It is quick and suits simple layouts where widgets sit in a single column or row, such as a title, a list and a status bar down the window. The grid layout manager places widgets in numbered rows and columns like a table, which makes it far better for forms where a column of labels must line up neatly against a column of entry boxes. Grid also allows a widget to span several columns and to be aligned within its cell. The two managers must never be used within the same container, because tkinter will hang without producing an error message.
4 Write a tkinter program that displays a text box and a button, and when the button is clicked shows the entered text in a label. 6
One mark for each of the following, to a maximum of 6.
- Imports tkinter and creates the main window
- Creates an Entry widget and displays it
- Creates a Label to show the result
- Defines a function that reads the entry
- Updates the label using config or a StringVar
- Attaches the function to the button with command and starts mainloop
Model answer. import tkinter as tk def show_text(): result.config(text="You typed: " + entry.get()) window = tk.Tk() window.title("Echo") entry = tk.Entry(window, width=30) entry.pack(pady=10) tk.Button(window, text="Show", command=show_text).pack() result = tk.Label(window, text="") result.pack(pady=10) window.mainloop() The function is passed to command without brackets, so it is called when the button is clicked rather than immediately when the program starts. The function reads the current contents of the entry with get and updates the label using config. Every widget is packed, otherwise none of them would appear.
5 Explain why input taken from a tkinter Entry widget should be validated before it is used. 3
One mark for each of the following, to a maximum of 3.
- Entry.get() always returns a string, whatever the user typed
- The user may type letters or leave it blank when a number is expected
- Without validation the program may crash or produce incorrect results
Model answer. The get method of an Entry widget always returns a string, no matter what the user typed, so any value intended as a number must be converted before it can be used in a calculation. Users can type anything at all into an entry box, including letters, symbols or nothing, so if the program calls int on the contents without checking it first, a ValueError will be raised and the application will crash. Validating the input, for example by checking isdigit and then checking the value falls within an acceptable range, allows the program to display a clear message asking the user to correct the entry instead, which is what makes the application robust.