What is a callback function in the context of a Button class?
A callback function is a function that you assign to a button to be called automatically when the button is pressed or released. It lets the program respond to button events without constantly checking the button state.
Click to reveal answer
beginner
Why use a Button class with callbacks instead of checking button state in a loop?
Using callbacks lets your program wait for button presses without wasting time checking repeatedly. It makes the code cleaner and more efficient, like setting an alarm instead of watching the clock all the time.
Click to reveal answer
beginner
How do you assign a callback function to a Button object in Python on Raspberry Pi?
You create a Button object and then use its method (like when_pressed) to assign your function. For example: button.when_pressed = my_function
Click to reveal answer
intermediate
What happens if you assign multiple callbacks to the same button event?
Usually, the last callback assigned will replace the previous one, so only one function runs when the button is pressed. To run multiple actions, you can write one callback that calls other functions.
Click to reveal answer
intermediate
Explain debouncing in the context of button callbacks.
Debouncing is a way to ignore quick repeated signals caused by the button's mechanical parts bouncing. Without debouncing, your callback might run many times for one press. Libraries often handle this automatically.
Click to reveal answer
What is the main purpose of a callback function in a Button class?
ATo automatically run code when the button is pressed
BTo check the button state continuously
CTo create a new button on the screen
DTo disable the button
✗ Incorrect
A callback function runs automatically when the button event happens, like a press.
How do you assign a callback function to a button press event in Python on Raspberry Pi?
Abutton.press = my_function()
Bbutton.on_press(my_function)
Cbutton.callback(my_function)
Dbutton.when_pressed = my_function
✗ Incorrect
The correct syntax is assigning the function to button.when_pressed without parentheses.
What problem does debouncing solve in button callbacks?
AIt prevents multiple triggers from one press
BIt makes the button press faster
CIt changes the button color
DIt disables the button
✗ Incorrect
Debouncing stops multiple signals caused by mechanical bouncing.
If you assign two different functions to button.when_pressed one after another, what happens?
ABoth functions run in order
BOnly the last assigned function runs
CNeither function runs
DThe first function runs twice
✗ Incorrect
The last assignment replaces the previous callback.
Why is using callbacks better than checking button state in a loop?
ACallbacks require more code
BCallbacks make the button press slower
CCallbacks save CPU time and make code cleaner
DCallbacks disable the button
✗ Incorrect
Callbacks let the program wait for events efficiently without constant checking.
Describe how to create a Button object with a callback function that runs when the button is pressed on a Raspberry Pi.
Think about how you tell the button what to do when pressed.
You got /3 concepts.
Explain what debouncing is and why it is important when using button callbacks.
Imagine pressing a button and it acting like you pressed it many times quickly.
You got /3 concepts.
Practice
(1/5)
1.
What is the main purpose of using a Button class with callbacks in Raspberry Pi programming?
easy
A. To run specific functions automatically when the button is pressed or released
B. To make the button physically larger
C. To change the button's color on the screen
D. To connect the button to the internet
Solution
Step 1: Understand what callbacks do
Callbacks are functions that run automatically when an event happens, like pressing a button.
Step 2: Connect callbacks to button actions
The Button class uses callbacks to run code when the button is pressed or released, making the program respond to these events.
Final Answer:
To run specific functions automatically when the button is pressed or released -> Option A
Quick Check:
Callbacks run functions on events = A [OK]
Hint: Callbacks run code on button press/release events [OK]
Common Mistakes:
Thinking callbacks change button size or color
Confusing callbacks with hardware features
Assuming callbacks connect buttons to the internet
2.
Which of the following is the correct way to assign a function my_function to run when a button is pressed using a Button class?
button = Button(17)
# What goes here?
easy
A. button.on_press(my_function)
B. button.on_press = my_function()
C. button.on_press = my_function
D. button.on_press: my_function
Solution
Step 1: Understand function assignment
Assigning a function as a callback means giving the function name without parentheses, so it can be called later.
Step 2: Check syntax for callback assignment
Using button.on_press = my_function assigns the function correctly. Adding parentheses calls it immediately, which is wrong.
Final Answer:
button.on_press = my_function -> Option C
Quick Check:
Assign function without () for callback = A [OK]
Hint: Assign function name without () for callbacks [OK]
Common Mistakes:
Adding parentheses calls function immediately
Using parentheses like a function call in assignment
Using incorrect syntax like colons or parentheses
3.
What will be the output when the following code runs and the button is pressed once?
A. Button class is missing the on_release callback
B. on_press is called without checking if it is None, causing an error
C. The press method should not call on_press at all
D. The button object is not created correctly
Solution
Step 1: Analyze the press method
The press method calls self.on_press() without checking if on_press is set.
Step 2: Identify the error cause
Since on_press is None by default and never assigned, calling None() causes a TypeError.
Final Answer:
on_press is called without checking if it is None, causing an error -> Option B
Quick Check:
Calling None as function causes error = B [OK]
Hint: Always check callback is set before calling it [OK]
Common Mistakes:
Ignoring None check before calling callback
Assuming on_press is set automatically
Thinking missing on_release causes this error
5.
You want to create a Button class that supports both on_press and on_release callbacks. Which code correctly implements this so pressing and releasing the button calls the right functions?
class Button:
def __init__(self):
# What should go here?
def press(self):
# What should go here?
def release(self):
# What should go here?
button = Button()
button.on_press = lambda: print('Pressed')
button.on_release = lambda: print('Released')
button.press()
button.release()
hard
A. Initialize on_press and on_release as True; check if True before calling
B. Initialize on_press and on_release to empty strings; print them in press and release
C. Do not initialize callbacks; call them directly in press and release methods
D. Initialize on_press and on_release to None; call them if set in press and release methods
Solution
Step 1: Initialize callbacks properly
Set self.on_press and self.on_release to None in __init__ to indicate no function assigned yet.
Step 2: Call callbacks safely in methods
In press and release, check if the callback is not None before calling it to avoid errors.
Final Answer:
Initialize on_press and on_release to None; call them if set in press and release methods -> Option D
Quick Check:
Initialize None and check before call = D [OK]
Hint: Set callbacks to None and check before calling [OK]
Common Mistakes:
Using wrong default values like empty strings or True
Calling callbacks without checking if assigned
Not supporting both press and release callbacks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.