-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
72 lines (58 loc) · 2.09 KB
/
example.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
from kivy.metrics import dp
from kivymd.app import MDApp
from kivymd.uix.datatables import MDDataTable
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.floatlayout import MDFloatLayout
from kivymd.uix.button import MDRaisedButton
class Example(MDApp):
data_tables = None
def build(self):
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Orange"
layout = MDFloatLayout() # root layout
# Creating control buttons.
button_box = MDBoxLayout(
pos_hint={"center_x": 0.5},
adaptive_size=True,
padding="24dp",
spacing="24dp",
)
for button_text in ["Add row", "Remove row"]:
button_box.add_widget(
MDRaisedButton(
text=button_text, on_release=self.on_button_press
)
)
# Create a table.
self.data_tables = MDDataTable(
pos_hint={"center_y": 0.5, "center_x": 0.5},
size_hint=(0.9, 0.6),
use_pagination=False,
column_data=[
("No.", dp(30)),
("Column 1", dp(40)),
("Column 2", dp(40)),
("Column 3", dp(40)),
],
row_data=[("1", "1", "2", "3")],
)
# Adding a table and buttons to the toot layout.
layout.add_widget(self.data_tables)
layout.add_widget(button_box)
return layout
def on_button_press(self, instance_button: MDRaisedButton) -> None:
'''Called when a control button is clicked.'''
try:
{
"Add row": self.add_row,
"Remove row": self.remove_row,
}[instance_button.text]()
except KeyError:
pass
def add_row(self) -> None:
last_num_row = int(self.data_tables.row_data[-1][0])
self.data_tables.add_row((str(last_num_row + 1), "1", "2", "3"))
def remove_row(self) -> None:
if len(self.data_tables.row_data) > 1:
self.data_tables.remove_row(self.data_tables.row_data[-1])
Example().run()