-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_gui.py
92 lines (72 loc) · 2.33 KB
/
simple_gui.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# main.py -- put your code here!
import display
import lvgl as lv
import time
# const() helps micropython to be more efficient
PADDING = const(30)
BTN_HEIGHT = const(80)
HOR_RES = const(480)
VER_RES = const(800)
class CounterScreen:
def __init__(self):
# counter to increase/decrease
self.counter = 0
# text we will use in label
self.lbl_template = "Counter value is %d"
def add_button(self, text, callback, y=500):
""" function that creates a button """
scr = lv.scr_act()
btn = lv.btn(scr)
btn.set_width(HOR_RES-2*PADDING);
btn.set_height(BTN_HEIGHT);
btn_lbl = lv.label(btn)
btn_lbl.set_text(text)
btn_lbl.set_align(lv.label.ALIGN.CENTER)
btn.align(scr, lv.ALIGN.IN_TOP_MID, 0, 0)
btn.set_y(y)
btn.set_event_cb(callback)
def draw(self):
# active screen (created during display init)
scr = lv.scr_act()
# create a label
self.lbl = lv.label(scr)
self.lbl.set_text(self.lbl_template % self.counter)
self.lbl.set_long_mode(lv.label.LONG.BREAK)
self.lbl.set_width(HOR_RES-2*PADDING)
self.lbl.set_x(PADDING)
self.lbl.set_align(lv.label.ALIGN.CENTER)
self.lbl.set_y(200)
# callback for "increase" button
def plus_one(btn, e):
if e == lv.EVENT.RELEASED:
self.counter += 1
self.lbl.set_text(self.lbl_template % self.counter)
# callback for "increase" button
def minus_one(btn, e):
if e == lv.EVENT.RELEASED:
self.counter -= 1
self.lbl.set_text(self.lbl_template % self.counter)
self.add_button("Increase counter", plus_one, 600)
self.add_button("Decrease counter", minus_one, 700)
def set_theme():
# setup theme
th = lv.theme_material_init(210, lv.font_roboto_22)
lv.theme_set_current(th)
def ioloop(dt):
while True:
time.sleep(dt/1000)
display.update(dt)
# in global scope such that it is accessible from REPL
counter_scr = CounterScreen()
def init():
display.init()
def main():
set_theme()
counter_scr.draw()
time.sleep(0.1)
ioloop(30)
if __name__ == '__main__':
init()
time.sleep(0.1)
main()
# that's it, now from REPL you can play with the gui if you want to.