0
0
Raspberry Piprogramming~5 mins

Button class with callbacks in Raspberry Pi - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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
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
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
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
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
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.