← VCE Software Development U3O1

GUI Button control and event handlers

Makes a button that runs the function show when clicked. Write the function name with no brackets, or it runs straight away instead of on click.

Understanding GUI Button control and event handlers in VCE Software Development U3O1

What it does: command=show hands tkinter the function itself to call later. Writing show() would run it immediately. The handler reads the entry's current text with ent.get() and then changes the label's text using lbl.config(). A handler is an ordinary function that waits to be called by tkinter when the button is clicked. The Entry control's .get() method returns the text the user typed. Entry has no .text attribute to read. Python must know the function name before it is used in command, so the def must come before the tk.Button line.

Common mistakes: Brackets after the function name call go() straight away, before any click. It should be command=go with no brackets so it runs only when the button is clicked. command needs the function itself, not a string. It should be command=hi with no quotes and no brackets.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 125–141) and word search (Modules 114–141).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line correctly makes a button that runs the function show when clicked?
tk.Button(root, text='Go', command=show())tk.Button(root, text='Go', command=show)
Matches
tk.Button(root, text='Go', command=show())
↓
Brackets after show make the function run immediately when the button is created, not when it is clicked. Use command=show.
Applying
Meanings
Term
tk.Button(root, text='Go', command=show)
Meaning
Makes a button that runs the function show when clicked. Write the function name with no brackets, or it runs straight away instead of on click.
Practice
import tkinter as tk
Testing
Gaps
b = tk.Button(root, text='Go', command=)
Check Answer
Spots
2 print('Hi')
3b = tk.Button(r, command=go())

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Puffin' badge — Definition Diver, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which line correctly makes a button that runs the function show when clicked?
A. tk.Button(root, text='Go', command=show())
B. tk.Button(root, text='Go', command=show) ✓
Write the function name with no brackets. Brackets call show() straight away, when the button is created, instead of waiting for a click.
2. What does the command option of a tk.Button do?
A. It sets the function that runs when the button is clicked ✓
B. It sets the text shown on the button
command links the button to an event handler function. The text option controls the label shown on the button.
3. Which statement about an event handler function is correct?
A. It is defined with def and runs only when the event happens, such as a click ✓
B. It runs once as soon as the window opens, before any click
A handler is an ordinary function that waits to be called by tkinter when the button is clicked.
4. A student writes command=save() and the save code runs as the window opens. Why?
A. The brackets call save immediately, so the button gets its result, not the function ✓
B. Buttons always run their command once when they are created
Without brackets, command=save passes the function itself to the button. With brackets it runs at once and the click does nothing.
5. Inside a handler, which line reads the text typed into an Entry called name_entry?
A. name = name_entry.text
B. name = name_entry.get() ✓
The Entry control's .get() method returns the text the user typed. Entry has no .text attribute to read.

Match the Pairs

tk.Button(root, text='Save', command=save_data) → Creates a button labelled Save that runs the function save_data each time it is clicked
def on_click(): → Defines an event handler function that will run when the user clicks the button
Pass the handler to command by name only, with no brackets, so it runs on click and not straight away → btn = tk.Button(root, text='Go', command=show)
A handler can read text from an Entry with .get() and update a Label with .config() → def greet(): lbl.config(text='Hi ' + ent.get())
tk.Button(root, text='Go', command=show()) → Brackets after show make the function run immediately when the button is created, not when it is clicked. Use command=show.
tk.Button(root, text='Go', command=show) def show(): print('Hi') → The button is created before show is defined, so Python raises a NameError. Define the handler function first, then create the button.

Key Term

tk.Button(root, text='Go', command=show)
Makes a button that runs the function show when clicked. Write the function name with no brackets, or it runs straight away instead of on click.

Fill in the Blanks

1. b = tk.Button(root, text='Go', command=show)
Hint: Type the name of the handler function, with no brackets after it
2. b = tk.Button(root, text='Go', command=show)
Hint: Type the Button option that tells it which function to run when clicked
3. def greet(): label.config(text='Hi!') b = tk.Button(root, text='Go', command=greet)
Hint: Type the name of the event handler function defined above, with no brackets
4. def greet(): label.config(text=entry.get()) b = tk.Button(root, text='Go', command=greet)
Hint: Type the Entry method that returns the text the user typed
5. def show(): label.config(text='Clicked!') b = tk.Button(root, text='Go', command=show)
Hint: Type the Label method used to change its text inside the handler

Spot the Error

Question 1
A def go():
B print('Hi')
C b = tk.Button(r, command=go())
D b.pack()
Line C — Brackets after the function name call go() straight away, before any click. It should be command=go with no brackets so it runs only when the button is clicked.
Question 2
A def hi():
B name = ent.get()
C lbl.config(text=name)
D b = tk.Button(r, command='hi')
E b.pack()
Line D — command needs the function itself, not a string. It should be command=hi with no quotes and no brackets.
Question 3
A root = tk.Tk()
B lbl = tk.Label(root, text='')
C def show()
D lbl.config(text='Clicked')
E lbl.pack()
Line C — A function header must end with a colon. It should be def show(): so the indented handler body belongs to the function.
Question 4
A ent = tk.Entry(root)
B def greet():
C name = ent.get
D lbl.config(text=name)
Line C — Without brackets, ent.get is only the method itself and does not read the Entry text. It should be ent.get() to call it and return what the user typed.
Question 5
A def show():
B lbl.text = 'Done'
C b = tk.Button(r, command=show)
D b.pack()
Line B — Setting lbl.text does not change what the label displays. Use lbl.config(text='Done') to update the label's text.

Student with a class code? Join your class →