-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcounter_with_lock.py
49 lines (42 loc) · 1.08 KB
/
counter_with_lock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from castella import (
App,
Button,
Column,
Component,
Row,
Spacer,
State,
Switch,
Text,
TextAlign,
)
from castella.frame import Frame
is_locked = State(False)
class Counter(Component):
def __init__(self, lock):
super().__init__()
self._is_locked = lock
def view(self):
count = State(0)
up = lambda _: count.set(count() + 1) if not self._is_locked() else ...
down = lambda _: count.set(count() - 1) if not self._is_locked() else ...
return Column(
Text(count),
Row(
Button("+", font_size=50).on_click(up),
Button("-", font_size=50).on_click(down),
),
)
App(
Frame("Counter", 800, 600),
Column(
Row(
Spacer(),
Text("Lock", align=TextAlign.RIGHT).fixed_width(80).erase_border(),
Switch(is_locked)
.fixed_width(80)
.on_change(lambda v: print("locked") if v else print("unlocked")),
).fixed_height(40),
Counter(is_locked),
),
).run()