Label#
The Class To use a custom color for You can use labels to display material design icons using the
The Warning For the MDLabel
widget is for rendering text.
MDLabel#
MDLabel
inherited from the Label
class
but for MDLabel
the text_size
parameter is (self.width, None)
and default is positioned on the left:from kivy.lang import Builder
from kivymd.app import MDApp
KV = '''
MDScreen:
MDLabel:
text: "MDLabel"
'''
class Test(MDApp):
def build(self):
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Orange"
return Builder.load_string(KV)
Test().run()
from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.screen import MDScreen
from kivymd.uix.label import MDLabel
class Test(MDApp):
def build(self):
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Orange"
return (
MDScreen(
MDLabel(
text="MDLabel"
)
)
)
Test().run()
MDLabel:
text: "MDLabel"
halign: "center"
MDLabel
color:#MDLabel
provides standard color themes for label color management:from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.label import MDLabel
KV = '''
MDBoxLayout:
orientation: "vertical"
'''
class Test(MDApp):
def build(self):
self.theme_cls.theme_style = "Dark"
screen = Builder.load_string(KV)
# Names of standard color themes.
for name_theme in [
"Primary",
"Secondary",
"Hint",
"Error",
"ContrastParentBackground",
]:
screen.add_widget(
MDLabel(
text=name_theme,
halign="center",
theme_text_color=name_theme,
)
)
return screen
Test().run()
MDLabel
, use a theme ‘Custom’.
After that, you can specify the desired color in the rgba
format
in the text_color
parameter:MDLabel:
text: "Custom color"
halign: "center"
theme_text_color: "Custom"
text_color: "blue"
MDLabel
provides standard font styles for labels. To do this,
specify the name of the desired style in the font_style
parameter:from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.label import MDLabel
from kivymd.font_definitions import theme_font_styles
KV = '''
MDScrollView:
MDList:
id: box
spacing: "8dp"
'''
class Test(MDApp):
def build(self):
self.theme_cls.theme_style = "Dark"
screen = Builder.load_string(KV)
# Names of standard font styles.
for name_style in theme_font_styles[:-1]:
screen.ids.box.add_widget(
MDLabel(
text=f"{name_style} style",
halign="center",
font_style=name_style,
adaptive_height=True,
)
)
return screen
Test().run()
Highlighting and copying labels#
You can highlight labels by double tap on the label:#
from kivy.lang.builder import Builder
from kivymd.app import MDApp
KV = '''
MDScreen:
MDLabel:
adaptive_size: True
pos_hint: {"center_x": .5, "center_y": .5}
text: "MDLabel"
allow_selection: True
padding: "4dp", "4dp"
'''
class Example(MDApp):
def build(self):
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Orange"
return Builder.load_string(KV)
Example().run()
from kivy.lang.builder import Builder
from kivymd.app import MDApp
from kivymd.uix.label import MDLabel
from kivymd.uix.screen import MDScreen
class Example(MDApp):
def build(self):
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Orange"
return (
MDScreen(
MDLabel(
adaptive_size=True,
pos_hint={"center_x": .5, "center_y": .5},
text="MDLabel",
allow_selection=True,
padding=("4dp", "4dp"),
)
)
)
Example().run()
You can copy the label text by double clicking on it:#
from kivy.lang.builder import Builder
from kivymd.app import MDApp
KV = '''
MDScreen:
MDLabel:
adaptive_size: True
pos_hint: {"center_x": .5, "center_y": .5}
text: "MDLabel"
padding: "4dp", "4dp"
allow_selection: True
allow_copy: True
on_copy: print("The text is copied to the clipboard")
'''
class Example(MDApp):
def build(self):
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Orange"
return Builder.load_string(KV)
Example().run()
from kivy.lang.builder import Builder
from kivymd.app import MDApp
from kivymd.uix.label import MDLabel
from kivymd.uix.screen import MDScreen
class Example(MDApp):
def build(self):
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Orange"
return (
MDScreen(
MDLabel(
id="label",
adaptive_size=True,
pos_hint={"center_x": .5, "center_y": .5},
text="MDLabel",
allow_selection=True,
allow_copy=True,
padding=("4dp", "4dp"),
)
)
)
def on_start(self):
self.root.ids.label.bind(on_copy=self.on_copy)
def on_copy(self, instance_label: MDLabel):
print("The text is copied to the clipboard")
Example().run()
MDIcon#
MDIcon
class.MDIcon
class is inherited from
MDLabel
and has the same parameters.MDIcon
class, you cannot use text
and font_style
options!MDIcon:
icon: "gmail"
pos_hint: {"center_x": .5, "center_y": .5}
MDIcon with badge icon#
MDIcon:
icon: "gmail"
badge_icon: "numeric-10"
pos_hint: {"center_x": .5, "center_y": .5}
API - kivymd.uix.label.label
#
- class kivymd.uix.label.label.MDLabel(*args, **kwargs)#
Label class.
For more information, see in the
DeclarativeBehavior
andThemableBehavior
andLabel
andMDAdaptiveWidget
andTouchBehavior
classes documentation.- Events:
- on_ref_press
Called when the user clicks on a word referenced with a
[ref]
tag in a text markup.- on_copy
Called when double-tapping on the label.
- on_selection
Called when double-tapping on the label.
- on_cancel_selection
Called when the highlighting is removed from the label text.
- font_style#
Label font style.
Available vanilla font_style are: ‘H1’, ‘H2’, ‘H3’, ‘H4’, ‘H5’, ‘H6’, ‘Subtitle1’, ‘Subtitle2’, ‘Body1’, ‘Body2’, ‘Button’, ‘Caption’, ‘Overline’, ‘Icon’.
font_style
is anStringProperty
and defaults to ‘Body1’.
- text#
Text of the label.
- theme_text_color#
Label color scheme name.
Available options are: ‘Primary’, ‘Secondary’, ‘Hint’, ‘Error’, ‘Custom’, ‘ContrastParentBackground’.
theme_text_color
is anOptionProperty
and defaults to None.
- text_color#
Label text color in (r, g, b, a) or string format.
text_color
is anColorProperty
and defaults to None.
- allow_copy#
Allows you to copy text to the clipboard by double-clicking on the label.
New in version 1.2.0.
allow_copy
is anBooleanProperty
and defaults to False.
- allow_selection#
Allows to highlight text by double-clicking on the label.
New in version 1.2.0.
allow_selection
is anBooleanProperty
and defaults to False.
- color_selection#
The color in (r, g, b, a) or string format of the text selection when the value of the
allow_selection
attribute is True.New in version 1.2.0.
color_selection
is anColorProperty
and defaults to None.
- color_deselection#
The color in (r, g, b, a) or string format of the text deselection when the value of the
allow_selection
attribute is True.New in version 1.2.0.
color_deselection
is anColorProperty
and defaults to None.
- is_selected#
Is the label text highlighted.
New in version 1.2.0.
is_selected
is anBooleanProperty
and defaults to False.
- parent_background#
- can_capitalize#
- canvas_bg#
- do_selection(self)#
- cancel_selection(self)#
- on_double_tap(self, touch, *args)#
Called by double clicking on the widget.
- on_window_touch(self, *args)#
- on_copy(self, *args)#
Called when double-tapping on the label.
New in version 1.2.0.
- on_selection(self, *args)#
Called when double-tapping on the label.
New in version 1.2.0.
- on_cancel_selection(self, *args)#
Called when the highlighting is removed from the label text.
New in version 1.2.0.
- on_opposite_colors(self, *args)#
- class kivymd.uix.label.label.MDIcon(*args, **kwargs)#
Icon class.
For more information, see in the
MDLabel
andMDFloatLayout
classes documentation.- icon#
Label icon name.
icon
is anStringProperty
and defaults to ‘android’.
- badge_icon#
Label badge icon name.
New in version 1.0.0.
badge_icon
is anStringProperty
and defaults to ‘’.
- badge_icon_color#
Badge icon color in (r, g, b, a) or string format.
New in version 1.0.0.
badge_icon_color
is anColorProperty
and defaults to None.
- badge_bg_color#
Badge icon background color in (r, g, b, a) or string format.
New in version 1.0.0.
badge_bg_color
is anColorProperty
and defaults to None.
- badge_font_size#
Badge font size.
New in version 1.0.0.
badge_font_size
is anNumericProperty
and defaults to 0.
- source#
Path to icon.
source
is anStringProperty
and defaults to None.
- adjust_size(self, *args)#