-
-
Notifications
You must be signed in to change notification settings - Fork 657
/
button.py
executable file
·2074 lines (1620 loc) · 61.7 KB
/
button.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Components/Button
=================
.. seealso::
`Material Design spec, Buttons <https://material.io/components/buttons>`_
`Material Design spec, Buttons: floating action button <https://material.io/components/buttons-floating-action-button>`_
.. rubric:: Buttons allow users to take actions, and make choices,
with a single tap.
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/buttons.png
:align: center
`KivyMD` provides the following button classes for use:
- MDIconButton_
- MDFloatingActionButton_
- MDFlatButton_
- MDRaisedButton_
- MDRectangleFlatButton_
- MDRectangleFlatIconButton_
- MDRoundFlatButton_
- MDRoundFlatIconButton_
- MDFillRoundFlatButton_
- MDFillRoundFlatIconButton_
- MDTextButton_
- MDFloatingActionButtonSpeedDial_
.. MDIconButton:
MDIconButton
------------
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-icon-button.gif
:align: center
.. code-block:: python
from kivy.lang import Builder
from kivymd.app import MDApp
KV = '''
MDScreen:
MDIconButton:
icon: "language-python"
pos_hint: {"center_x": .5, "center_y": .5}
'''
class Example(MDApp):
def build(self):
return Builder.load_string(KV)
Example().run()
The :class:`~MDIconButton.icon` parameter must have the name of the icon
from ``kivymd/icon_definitions.py`` file.
You can also use custom icons:
.. code-block:: kv
MDIconButton:
icon: "data/logo/kivy-icon-256.png"
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-icon-custom-button.gif
:align: center
By default, :class:`~MDIconButton` button has a size ``(dp(48), dp (48))``.
Use :class:`~BaseButton.user_font_size` attribute to resize the button:
.. code-block:: kv
MDIconButton:
icon: "android"
user_font_size: "64sp"
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-icon-button-user-font-size.gif
:align: center
By default, the color of :class:`~MDIconButton`
(depending on the style of the application) is black or white.
You can change the color of :class:`~MDIconButton` as the text color
of :class:`~kivymd.uix.label.MDLabel`:
.. code-block:: kv
MDIconButton:
icon: "android"
theme_text_color: "Custom"
text_color: app.theme_cls.primary_color
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-icon-button-theme-text-color.png
:align: center
.. MDFloatingActionButton:
MDFloatingActionButton
----------------------
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-floating-action-button.png
:align: center
The above parameters for :class:`~MDIconButton` apply
to :class:`~MDFloatingActionButton`.
To change :class:`~MDFloatingActionButton` background, use the
``md_bg_color`` parameter:
.. code-block:: kv
MDFloatingActionButton:
icon: "android"
md_bg_color: app.theme_cls.primary_color
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-floating-action-button-md-bg-color.png
:align: center
The length of the shadow is controlled by the ``elevation_normal`` parameter:
.. code-block:: kv
MDFloatingActionButton:
icon: "android"
elevation_normal: 12
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-floating-action-button-elevation-normal.png
:align: center
.. MDFlatButton:
MDFlatButton
------------
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-flat-button.gif
:align: center
To change the text color of: class:`~MDFlatButton` use the ``text_color`` parameter:
.. code-block:: kv
MDFlatButton:
text: "MDFLATBUTTON"
theme_text_color: "Custom"
text_color: 0, 0, 1, 1
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-flat-button-text-color.png
:align: center
Or use markup:
.. code-block:: kv
MDFlatButton:
text: "[color=#00ffcc]MDFLATBUTTON[/color]"
To specify the font size and font name, use the parameters as in the usual
`Kivy` buttons:
.. code-block:: kv
MDFlatButton:
text: "MDFLATBUTTON"
font_size: "18sp"
font_name: "path/to/font"
.. MDRaisedButton:
MDRaisedButton
--------------
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-raised-button.gif
:align: center
This button is similar to the :class:`~MDFlatButton` button except that you
can set the background color for :class:`~MDRaisedButton`:
.. code-block:: kv
MDRaisedButton:
text: "MDRAISEDBUTTON"
md_bg_color: 1, 0, 1, 1
.. MDRectangleFlatButton:
MDRectangleFlatButton
---------------------
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-rectangle-flat-button.gif
:align: center
.. code-block:: kv
MDRectangleFlatButton:
text: "MDRECTANGLEFLATBUTTON"
theme_text_color: "Custom"
text_color: 1, 0, 0, 1
line_color: 0, 0, 1, 1
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-rectangle-flat-button-md-bg-color.png
:align: center
.. MDRectangleFlatIconButton:
MDRectangleFlatIconButton
-------------------------
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-rectangle-flat-icon-button.png
:align: center
Button parameters :class:`~MDRectangleFlatButton` are the same as
button :class:`~MDRectangleFlatButton`:
.. code-block:: kv
MDRectangleFlatIconButton:
icon: "android"
text: "MDRECTANGLEFLATICONBUTTON"
theme_text_color: "Custom"
text_color: 0, 0, 1, 1
line_color: 1, 0, 1, 1
icon_color: 1, 0, 0, 1
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-rectangle-flat-icon-button-custom.png
:align: center
Without border
--------------
.. code-block:: python
from kivymd.app import MDApp
from kivymd.uix.screen import MDScreen
from kivymd.uix.button import MDRectangleFlatIconButton
class Example(MDApp):
def build(self):
screen = MDScreen()
screen.add_widget(
MDRectangleFlatIconButton(
text="MDRectangleFlatIconButton",
icon="language-python",
line_color=(0, 0, 0, 0),
pos_hint={"center_x": .5, "center_y": .5},
)
)
return screen
Example().run()
.. code-block:: kv
MDRectangleFlatIconButton:
text: "MDRectangleFlatIconButton"
icon: "language-python"
line_color: 0, 0, 0, 0
pos_hint: {"center_x": .5, "center_y": .5}
.. MDRoundFlatButton:
MDRoundFlatButton
-----------------
.. code-block:: kv
MDRoundFlatButton:
text: "MDROUNDFLATBUTTON"
text_color: 0, 1, 0, 1
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-round-flat-button-text-color.png
:align: center
.. MDRoundFlatIconButton:
MDRoundFlatIconButton
---------------------
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-round-flat-icon-button.png
:align: center
Button parameters :class:`~MDRoundFlatIconButton` are the same as
button :class:`~MDRoundFlatButton`:
.. code-block:: kv
MDRoundFlatIconButton:
icon: "android"
text: "MDROUNDFLATICONBUTTON"
.. MDFillRoundFlatButton:
MDFillRoundFlatButton
---------------------
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-fill-round-flat-button.png
:align: center
Button parameters :class:`~MDFillRoundFlatButton` are the same as
button :class:`~MDRaisedButton`.
.. MDFillRoundFlatIconButton:
MDFillRoundFlatIconButton
-------------------------
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-fill-round-flat-icon-button.png
:align: center
Button parameters :class:`~MDFillRoundFlatIconButton` are the same as
button :class:`~MDRaisedButton`.
.. note:: Notice that the width of the :class:`~MDFillRoundFlatIconButton`
button matches the size of the button text.
.. MDTextButton:
MDTextButton
------------
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-text-button.png
:align: center
.. code-block:: kv
MDTextButton:
text: "MDTEXTBUTTON"
custom_color: 0, 1, 0, 1
.. MDFloatingActionButtonSpeedDial:
MDFloatingActionButtonSpeedDial
-------------------------------
.. Note:: See the full list of arguments in the class
:class:`~MDFloatingActionButtonSpeedDial`.
.. code-block:: python
from kivy.lang import Builder
from kivymd.app import MDApp
KV = '''
MDScreen:
MDFloatingActionButtonSpeedDial:
data: app.data
root_button_anim: True
'''
class Example(MDApp):
data = {
'Python': 'language-python',
'PHP': 'language-php',
'C++': 'language-cpp',
}
def build(self):
return Builder.load_string(KV)
Example().run()
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/MDFloatingActionButtonSpeedDial.gif
:align: center
Or without KV Language:
.. code-block:: python
from kivymd.uix.screen import MDScreen
from kivymd.app import MDApp
from kivymd.uix.button import MDFloatingActionButtonSpeedDial
class Example(MDApp):
data = {
'Python': 'language-python',
'PHP': 'language-php',
'C++': 'language-cpp',
}
def build(self):
screen = MDScreen()
speed_dial = MDFloatingActionButtonSpeedDial()
speed_dial.data = self.data
speed_dial.root_button_anim = True
screen.add_widget(speed_dial)
return screen
Example().run()
You can use various types of animation of labels for buttons on the stack:
.. code-block:: kv
MDFloatingActionButtonSpeedDial:
hint_animation: True
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/MDFloatingActionButtonSpeedDial-hint.gif
:align: center
You can set your color values for background, text of buttons etc:
.. code-block:: kv
MDFloatingActionButtonSpeedDial:
bg_hint_color: app.theme_cls.primary_light
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/MDFloatingActionButtonSpeedDial-hint-color.png
:align: center
.. seealso::
`See full example <https://github.com/kivymd/KivyMD/wiki/Components-Button>`_
"""
__all__ = (
"MDIconButton",
"MDFloatingActionButton",
"MDFlatButton",
"MDRaisedButton",
"MDRectangleFlatButton",
"MDRectangleFlatIconButton",
"MDRoundFlatButton",
"MDRoundFlatIconButton",
"MDFillRoundFlatButton",
"MDFillRoundFlatIconButton",
"MDTextButton",
"MDFloatingActionButtonSpeedDial",
)
from kivy.animation import Animation
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.graphics.context_instructions import Color
from kivy.graphics.stencil_instructions import (
StencilPop,
StencilPush,
StencilUnUse,
StencilUse,
)
from kivy.graphics.vertex_instructions import Ellipse, RoundedRectangle
from kivy.lang import Builder
from kivy.metrics import dp
from kivy.properties import (
BooleanProperty,
ColorProperty,
DictProperty,
NumericProperty,
ObjectProperty,
OptionProperty,
StringProperty,
)
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivymd.color_definitions import text_colors
from kivymd.font_definitions import theme_font_styles
from kivymd.theming import ThemableBehavior
from kivymd.uix.behaviors import (
BackgroundColorBehavior,
CircularElevationBehavior,
CircularRippleBehavior,
CommonElevationBehavior,
FakeRectangularElevationBehavior,
RectangularRippleBehavior,
)
from kivymd.uix.label import MDLabel
from kivymd.uix.tooltip import MDTooltip
Builder.load_string(
"""
<BaseButton>
canvas:
Clear
Color:
rgba:
self.md_bg_color if not self.disabled else \
(root.md_bg_color_disabled if root.md_bg_color_disabled \
else root.theme_cls.disabled_hint_text_color)
RoundedRectangle:
size: self.size
pos: self.pos
radius: [root._radius, ]
lbl_txt: lbl_txt
size_hint: None, None
height: dp(20) + lbl_txt.texture_size[1]
width: lbl_txt.texture_size[0] + dp(24)
MDLabel:
id: lbl_txt
text: root.text
font_size: root.font_size
font_style: root.font_style
adaptive_size: True
-text_size: None, None
theme_text_color: root.theme_text_color
text_color: root.text_color
markup: True
disabled: root.disabled
opposite_colors: root.opposite_colors
font_name: root.font_name if root.font_name else self.font_name
<BaseRoundButton>
canvas:
Clear
Color:
rgba:
(self.md_bg_color if root.icon in md_icons else (0, 0, 0, 0)) \
if not root.disabled else \
(root.md_bg_color_disabled if root.md_bg_color_disabled \
else root.theme_cls.disabled_hint_text_color)
Ellipse:
size: self.size
pos: self.pos
source: self.source if hasattr(self, "source") else ""
size: "48dp", "48dp"
lbl_txt: lbl_txt
padding: "12dp" if root.icon in md_icons else (0, 0, 0, 0)
MDIcon:
id: lbl_txt
icon: root.icon
font_size: root.user_font_size if root.user_font_size else self.font_size
font_name: root.font_name if root.font_name else self.font_name
theme_text_color: root.theme_text_color
text_color:
root.text_color if not root.disabled else \
(root.md_bg_color_disabled if root.md_bg_color_disabled \
else root.theme_cls.disabled_hint_text_color)
disabled: root.disabled
valign: "middle"
halign: "center"
opposite_colors: root.opposite_colors
<MDRoundFlatButton>
canvas.before:
Clear
Color:
rgba:
(root.theme_cls.primary_color if not root.line_color else root.line_color) \
if not root.disabled else \
(root.md_bg_color_disabled if root.md_bg_color_disabled \
else root.theme_cls.disabled_hint_text_color)
Line:
width: root.line_width
rounded_rectangle:
(self.x, self.y, self.width, self.height,\
root._radius, root._radius, root._radius, root._radius,\
self.height)
theme_text_color: "Custom"
<MDFillRoundFlatButton>
canvas.before:
Clear
Color:
rgba:
(root.theme_cls.primary_color if root.md_bg_color == [0.0, 0.0, 0.0, 0.0] \
else root.md_bg_color) if not root.disabled else \
(root.md_bg_color_disabled if root.md_bg_color_disabled \
else root.theme_cls.disabled_hint_text_color)
RoundedRectangle:
size: self.size
pos: self.pos
radius: [root._radius, ]
line_width: 0.001
theme_text_color: "Custom"
<MDFillRoundFlatIconButton>
canvas.before:
Clear
Color:
rgba:
(root.theme_cls.primary_color if root.md_bg_color == [0.0, 0.0, 0.0, 0.0] \
else root.md_bg_color) if not root.disabled else \
(root.md_bg_color_disabled if root.md_bg_color_disabled \
else root.theme_cls.disabled_hint_text_color)
RoundedRectangle:
size: self.size
pos: self.pos
radius: [root._radius, ]
line_width: 0.001
theme_text_color: "Custom"
<MDRectangleFlatButton>
canvas.before:
Clear
Color:
rgba:
(root.theme_cls.primary_color if not root.line_color else root.line_color) \
if not root.disabled else \
(root.md_bg_color_disabled if root.md_bg_color_disabled \
else root.theme_cls.disabled_hint_text_color)
Line:
width: root.line_width
rectangle: (self.x, self.y, self.width, self.height)
theme_text_color: "Custom"
<MDRectangleFlatIconButton>
canvas.before:
Clear
Color:
rgba:
(root.theme_cls.primary_color if not root.line_color else root.line_color) \
if not root.disabled else \
(root.md_bg_color_disabled if root.md_bg_color_disabled \
else root.theme_cls.disabled_hint_text_color)
Line:
width: root.line_width
rectangle: (self.x, self.y, self.width, self.height)
size_hint_x: None
width: root.lbl_txt.width + lbl_ic.width * 2 + box.spacing
MDBoxLayout:
id: box
size_hint: None, None
size: root.size
padding: "10dp", 0, 0, 0
spacing: "8dp"
MDIcon:
id: lbl_ic
icon: root.icon
theme_text_color: "Custom"
text_color:
(root.theme_cls.primary_color if not root.icon_color else root.icon_color) \
if not root.disabled else \
(root.md_bg_color_disabled if root.md_bg_color_disabled \
else root.theme_cls.disabled_hint_text_color)
size_hint_x: None
width: self.texture_size[0]
MDLabel:
id: lbl
text: root.text
font_size: root.font_size
font_style: root.font_style
adaptive_size: True
-text_size: None, None
theme_text_color: "Custom"
text_color: root.text_color
markup: True
disabled: root.disabled
opposite_colors: root.opposite_colors
font_name: root.font_name if root.font_name else self.font_name
pos_hint: {"center_y": .5}
<MDRoundFlatIconButton>
canvas.before:
Clear
Color:
rgba:
(root.theme_cls.primary_color if not root.line_color else root.line_color) \
if not root.disabled else \
(root.md_bg_color_disabled if root.md_bg_color_disabled \
else root.theme_cls.disabled_hint_text_color)
Line:
width: root.line_width
rounded_rectangle:
(self.x, self.y, self.width, self.height,\
root._radius, root._radius, root._radius, root._radius,\
self.height)
size_hint_x: None
width: root.lbl_txt.width + lbl_ic.width * 2 + box.spacing
MDBoxLayout:
id: box
size_hint: None, None
size: root.size
padding: "10dp", 0, 0, 0
spacing: "8dp"
MDIcon:
id: lbl_ic
icon: root.icon
theme_text_color: "Custom"
text_color:
(root.theme_cls.primary_color if not root.icon_color else root.icon_color) \
if not root.disabled else \
(root.md_bg_color_disabled if root.md_bg_color_disabled \
else root.theme_cls.disabled_hint_text_color)
size_hint_x: None
width: self.texture_size[0]
MDLabel:
id: lbl
text: root.text
font_size: root.font_size
font_style: root.font_style
adaptive_size: True
-text_size: None, None
theme_text_color: root.theme_text_color
text_color: root.text_color
markup: True
disabled: root.disabled
opposite_colors: root.opposite_colors
font_name: root.font_name if root.font_name else self.font_name
pos_hint: {"center_y": .5}
<MDIconButton>
on_size: root.set_size(0)
<MDFloatingActionButton>
theme_text_color: "Custom"
# theme_text_color: root.theme_text_color
# opposite_colors: root.opposite_colors
on_size: root.set_size(0)
<MDTextButton>
adaptive_size: True
color: root.theme_cls.primary_color if not root.color else root.color
opacity: 1
<BaseFloatingBottomButton>
theme_text_color: "Custom"
md_bg_color: self.theme_cls.primary_color
canvas.before:
Color:
rgba:
self.theme_cls.primary_color \
if not self._bg_color else self._bg_color
RoundedRectangle:
pos:
(self.x - self._canvas_width + dp(1.5)) + self._padding_right / 2, \
self.y - self._padding_right / 2 + dp(1.5)
size:
self.width + self._canvas_width - dp(3), \
self.height + self._padding_right - dp(3)
radius: [self.height / 2]
<BaseFloatingRootButton>
theme_text_color: "Custom"
md_bg_color: self.theme_cls.primary_color
canvas.before:
PushMatrix
Rotate:
angle: self._angle
axis: (0, 0, 1)
origin: self.center
canvas.after:
PopMatrix
<BaseFloatingLabel>
size_hint: None, None
padding: "8dp", "4dp", "8dp", "4dp"
height: label.texture_size[1] + self.padding[1] * 2
width: label.texture_size[0] + self.padding[0] * 2
elevation: 10
canvas:
Color:
rgba:
self.theme_cls.primary_color \
if not root.bg_color else root.bg_color
RoundedRectangle:
pos: self.pos
size: self.size
radius: [5]
Label:
id: label
markup: True
text: root.text
size_hint: None, None
size: self.texture_size
color: root.theme_cls.text_color if not root.text_color else root.text_color
"""
)
class BaseButton(ThemableBehavior, ButtonBehavior, AnchorLayout):
"""Base class for all buttons."""
text = StringProperty(" ")
"""
Button text.
:attr:`text` is an :class:`~kivy.properties.StringProperty`
and defaults to `' '`.
"""
theme_text_color = OptionProperty(
"Primary",
options=[
"Primary",
"Secondary",
"Hint",
"Error",
"Custom",
"ContrastParentBackground",
],
)
"""
Button text type. Available options are: (`"Primary"`, `"Secondary"`,
`"Hint"`, `"Error"`, `"Custom"`, `"ContrastParentBackground"`).
:attr:`theme_text_color` is an :class:`~kivy.properties.OptionProperty`
and defaults to `'Primary'`.
"""
text_color = ColorProperty(None)
"""
Button text color in (r, g, b, a) format.
:attr:`text_color` is an :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
"""
font_name = StringProperty()
"""
Button text font name.
:attr:`font_name` is an :class:`~kivy.properties.StringProperty`
and defaults to `''`.
"""
font_size = NumericProperty("14sp")
"""
Button text font size.
:attr:`font_size` is an :class:`~kivy.properties.NumericProperty`
and defaults to `14sp`.
"""
user_font_size = NumericProperty(0)
"""
Custom font size for :class:`~MDIconButton`.
:attr:`user_font_size` is an :class:`~kivy.properties.NumericProperty`
and defaults to `0`.
"""
font_style = OptionProperty("Body1", options=theme_font_styles)
"""
Button text font style.
Available vanilla font_style are: `'H1'`, `'H2'`, `'H3'`, `'H4'`, `'H5'`,
`'H6'`, `'Subtitle1'`, `'Subtitle2'`, `'Body1'`, `'Body2'`, `'Button'`,
`'Caption'`, `'Overline'`, `'Icon'`.
:attr:`font_style` is an :class:`~kivy.properties.StringProperty`
and defaults to `'Body1'`.
"""
md_bg_color = ColorProperty([1.0, 1.0, 1.0, 0])
"""
Button background color.
:attr:`md_bg_color` is an :class:`~kivy.properties.ColorProperty`
and defaults to `[1.0, 1.0, 1.0, 0]`.
"""
md_bg_color_disabled = ColorProperty(None)
"""
Disabled button text color.
:attr:`md_bg_color_disabled` is an :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
"""
_radius = NumericProperty(0)
_md_bg_color = ColorProperty(None) # last current button color
def __init__(self, **kwargs):
super().__init__(**kwargs)
if not self.md_bg_color_disabled:
self.md_bg_color_disabled = self.theme_cls.disabled_hint_text_color
self.theme_cls.bind(primary_palette=self.update_md_bg_color)
self.theme_cls.bind(theme_style=self.update_text_color)
Clock.schedule_once(self.set_md_bg_color)
if not self.text_color:
self.text_color = self.theme_cls.text_color
def update_text_color(self, instance, value):
pass
def set_md_bg_color(self, interval):
"""Checks if a value is set for the `md_bg_color` parameter."""
if self.md_bg_color == [1.0, 1.0, 1.0, 0]:
self.md_bg_color = self.theme_cls.primary_color
if not self.md_bg_color_disabled:
self.md_bg_color_disabled = self.theme_cls.disabled_hint_text_color
def update_md_bg_color(self, instance, value):
"""Called when the application color palette changes."""
self.md_bg_color = self.theme_cls._get_primary_color()
self.update_text_color(instance, value)
def on_disabled(self, instance, value):
"""
Sets the color of the button at the moment of setting the parameter
`disabled`.
"""
# Sets button background color (disabled).
if value:
if not self.md_bg_color_disabled:
self.md_bg_color_disabled = (
self.theme_cls.disabled_hint_text_color
)
self.md_bg_color = self.md_bg_color_disabled
# Sets last current button background color.
else:
self.md_bg_color = (
self._md_bg_color
if self._md_bg_color
else self.theme_cls.primary_color
)
def on_md_bg_color(self, instance, value):
"""Sets last current button background color."""
if self.md_bg_color != self.md_bg_color_disabled:
self._md_bg_color = value
class BasePressedButton(BaseButton):
"""
Base class for those button which fade to a background color on press.
"""
animation_fade_bg = None
current_md_bg_color = ColorProperty(None)
def on_touch_down(self, touch):
if touch.is_mouse_scrolling:
return False
elif not self.collide_point(touch.x, touch.y):
return False
elif self in touch.ud:
return False
elif self.disabled:
return False
else:
if self.md_bg_color == [0.0, 0.0, 0.0, 0.0]:
self.current_md_bg_color = self.md_bg_color
self.animation_fade_bg = Animation(
duration=0.5, md_bg_color=[0.0, 0.0, 0.0, 0.1]
)
self.animation_fade_bg.start(self)
return super().on_touch_down(touch)
def on_touch_up(self, touch):
if (
self.collide_point(touch.x, touch.y)
and self.animation_fade_bg
and not self.disabled
):
self.animation_fade_bg.stop_property(self, "md_bg_color")
Animation(
duration=0.05, md_bg_color=self.current_md_bg_color
).start(self)
return super().on_touch_up(touch)
class BaseRectangularButton(
RectangularRippleBehavior,
FakeRectangularElevationBehavior,
BackgroundColorBehavior,
BasePressedButton,
BaseButton,
):
"""Base class for all rectangular buttons."""
def on_width(self, *args):
if self.width < 88:
self.width = 88
class BaseFlatButton(BaseRectangularButton):
def update_md_bg_color(self, instance, value):
"""Called when the application color palette changes."""