2022-11-22 14:37:51 +01:00
|
|
|
class CounterBtn():
|
|
|
|
def __init__(self):
|
|
|
|
self.cnt = 0
|
|
|
|
#
|
|
|
|
# Create a button with a label and react on click event.
|
|
|
|
#
|
|
|
|
|
2023-10-12 20:37:27 +02:00
|
|
|
button = lv.button(lv.screen_active()) # Add a button the current screen
|
2023-09-14 20:12:31 +02:00
|
|
|
button.set_pos(10, 10) # Set its position
|
|
|
|
button.set_size(120, 50) # Set its size
|
|
|
|
button.align(lv.ALIGN.CENTER,0,0)
|
|
|
|
button.add_event(self.button_event_cb, lv.EVENT.ALL, None) # Assign a callback to the button
|
|
|
|
label = lv.label(button) # Add a label to the button
|
2022-11-22 14:37:51 +01:00
|
|
|
label.set_text("Button") # Set the labels text
|
|
|
|
label.center()
|
|
|
|
|
2023-09-14 20:12:31 +02:00
|
|
|
def button_event_cb(self,e):
|
2023-02-20 20:50:58 +01:00
|
|
|
code = e.get_code()
|
2023-09-14 20:12:31 +02:00
|
|
|
button = e.get_target_obj()
|
2022-11-22 14:37:51 +01:00
|
|
|
if code == lv.EVENT.CLICKED:
|
|
|
|
self.cnt += 1
|
|
|
|
|
2023-07-31 16:21:49 +08:00
|
|
|
# Get the first child of the button which is the label and change its text
|
2023-09-14 20:12:31 +02:00
|
|
|
label = button.get_child(0)
|
2023-07-31 16:21:49 +08:00
|
|
|
label.set_text("Button: " + str(self.cnt))
|
2022-11-22 14:37:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
counterBtn = CounterBtn()
|
2021-06-07 13:56:08 +02:00
|
|
|
|