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
|
"""
Part of the tagit module.
A copy of the license is provided with the project.
Author: Matthias Baumgartner, 2022
"""
# standard imports
import os
# kivy imports
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.relativelayout import RelativeLayout
import kivy.properties as kp
# exports
__all__ = ('Tile', 'TileWithLabel', 'TileTabular')
## code ##
# load kv
Builder.load_file(os.path.join(os.path.dirname(__file__), 'tile.kv'))
# classes
class Tile(RelativeLayout):
visible = kp.BooleanProperty(False)
root = kp.ObjectProperty(None)
def on_visible(self, wx, visible):
if visible:
self.update()
def update(self, *args, **kwargs):
abstract()
class TileWithLabel(Tile):
pass
class TileTabularRow(BoxLayout):
pass
class TileTabularCell(Label):
pass
class TileTabular(Tile):
tabledata = kp.ObjectProperty()
keywidth = kp.NumericProperty(0.5)
def on_tabledata(self, wx, data):
# set items
self.rows.clear_widgets()
for t_key, t_value in data.items():
# row
row = TileTabularRow()
# left column (keys)
key = TileTabularCell(
text=t_key,
halign='right',
font_size = self.font_size,
size_hint=(None, 1),
width=self.width * self.keywidth if self.keywidth < 1 else self.keywidth,
)
# right column (values)
value = TileTabularCell(
text=str(t_value),
halign='left',
font_size = self.font_size,
size_hint=(1, None),
)
# adjust key's width and height dynamically.
# value's width and height are adjusted automatically
self.bind(width=lambda wx, width, key=key: setattr(key, 'width',
width * self.keywidth if self.keywidth < 1 else self.keywidth))
key.bind(height=lambda wx, height, key=key: setattr(key, 'text_size',
(key.width, height)))
# add widgets
row.add_widget(key)
row.add_widget(value)
self.rows.add_widget(row)
## EOF ##
|