Kivy is a platform-independent GUI tool in Python. As it can be run on Android, iOS, Linux and Windows, etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktop applications.
Label widget –
The Label widget is for rendering text. It supports ASCII and unicode strings. The label is the text which we want to add to our window, give to the buttons, and so on. On labels, we can apply the styling also i.e increase text, size, color, and more.
Let’s see how to add Label to a Kivy window.
Kivy Tutorial – Learn Kivy with Examples.
How to add a label ?
1) import kivy
2) import kivy App
3) import label
4) set minimum version (optional)
5) Extend the App class
6) overwrite the build function
7) Add and return label
8) Run the instance of class
Below is the code:
Python3
import kivy
kivy.require( "1.9.1" )
from kivy.app import App
from kivy.uix.label import Label
class MyLabelApp(App):
def build( self ):
lbl = Label(text = "Label is Added on screen !!:):)" )
return lbl
label = MyLabelApp()
label.run()
|
Output:

How to do Styling in label ?
Python3
l2 = Label(text = "Label is Added on \n screen !!:):)
and its Multi\nLine", font_size = '20sp' ,
color = ( 0.41 , 0.42 , 0.74 , 1 )
|
Output:

How to markup the text ?
You can change the style of the text using Text Markup. The syntax is similar to above syntax but some more things are there.
Python3
l2 = Label(text = "[color = ff3333][b] 'Label' [ / b] is Added [ / color]\n
[color = 3333ff ]Screen !!:):):):)[ / color]",
font_size = '20sp' , markup = True )
|
Output:

More markup tags we can use –
[b][/b] -> Activate bold text
[i][/i] -> Activate italic text
[u][/u] -> Underlined text
[s][/s] -> Strikethrough text
[font=][/font] -> Change the font
[size=][/size]] -> Change the font size
[color=#][/color] -> Change the text color
[ref=][/ref] -> Add an interactive zone. The reference + bounding box inside the reference will be available in Label.refs
[anchor=] -> Put an anchor in the text. You can get the position of your anchor within the text with Label.anchors
[sub][/sub] -> Display the text at a subscript position relative to the text before it.
[sup][/sup] -> Display the text at a superscript position relative to the text before it.
Reference: https://kivy.org/doc/stable/api-kivy.uix.label.html
Label using KivyMD
KivyMD is an extension of the Kivy framework. KivyMD is a collection of Material Design widgets for use with Kivy, a GUI framework for making mobile applications.
First, we will import MDLabel from kivymd.uix.label library
MDLabel has the following parameters-
- text- the text we want to put on the label
- halign-the position where we want to put the label.
- theme_text_color- the theme for text colors like custom, primary, secondary, hint, or error
- text_color- if theme_text_color is custom we can assign text color to an RGB tuple.
- font_style-like caption,headings
Below is the following example using MDLabel
Python3
from kivymd.app import MDApp
from kivymd.uix.label import MDLabel
from kivymd.uix.screen import Screen
class Demo(MDApp):
def build( self ):
screen = Screen()
l = MDLabel(text = "Welcome!" ,pos_hint = { 'center_x' : 0.8 ,
'center_y' : 0.8 },
theme_text_color = "Custom" ,
text_color = ( 0.5 , 0 , 0.5 , 1 ),
font_style = 'Caption' )
l1 = MDLabel(text = "Welcome!" , pos_hint = { 'center_x' : 0.8 ,
'center_y' : 0.5 },
theme_text_color = "Custom" ,
text_color = ( 0.5 , 0 , 0.5 , 1 ),
font_style = 'H2' )
l2 = MDLabel(text = "Welcome!" , pos_hint = { 'center_x' : 0.8 ,
'center_y' : 0.2 },
theme_text_color = "Custom" ,
text_color = ( 0.5 , 0 , 0.5 , 1 ),
font_style = 'H1' )
screen.add_widget(l)
screen.add_widget(l1)
screen.add_widget(l2)
return screen
if __name__ = = "__main__" :
Demo().run()
|

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
09 Nov, 2022
Like Article
Save Article