-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.py
103 lines (82 loc) · 2.59 KB
/
demo.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
93
94
95
96
97
98
99
100
101
102
103
# ruff: noqa: E402
"""Demonstrate the usage of the HeadlessWidget class."""
from __future__ import annotations
from pathlib import Path
import png
from headless_kivy import HeadlessWidget, config
WIDTH = 400
HEIGHT = 240
def render(
*,
regions: list[config.Region],
) -> None:
"""Render the data to a png file."""
data = regions[0]['data']
with Path('demo.png').open('wb') as file:
png.Writer(
alpha=True,
width=data.shape[1],
height=data.shape[0],
greyscale=False, # pyright: ignore [reportArgumentType]
bitdepth=8,
).write(
file,
data.reshape(data.shape[0], -1).tolist(),
)
config.setup_headless_kivy(
{
'callback': render,
'width': WIDTH,
'height': HEIGHT,
'flip_vertical': True,
'rotation': 3,
},
)
from kivy.app import App
from kivy.graphics.context_instructions import Color
from kivy.graphics.vertex_instructions import Ellipse
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
class FboGridLayout(GridLayout, HeadlessWidget):
"""The `HeadlessWidget` subclass for the `GridLayout` class."""
class DemoApp(App):
"""The demo application class."""
def build(self) -> FboGridLayout:
"""Build the demo application."""
fbg = FboGridLayout(rows=2, cols=2)
for index, color in enumerate(
[(1, 0, 0, 1), (0, 1, 0, 1), (0, 0, 1, 1), (1, 1, 0, 0)],
):
label = Label(text=f'Hello {index}')
with label.canvas.before:
Color(*color)
label.shape = Ellipse()
label.bind(
size=lambda label, *_: (
setattr(
label.shape,
'size',
(label.size[0] / 2, label.size[1] / 2),
),
setattr(
label.shape,
'pos',
(
label.pos[0] + label.size[0] / 4,
label.pos[1] + label.size[1] / 4,
),
),
),
pos=lambda label, *_: setattr(
label.shape,
'pos',
(
label.pos[0] + label.size[0] / 4,
label.pos[1] + label.size[1] / 4,
),
),
)
fbg.add_widget(label)
return fbg
if __name__ == '__main__':
DemoApp().run()