Skip to content
Related Articles

Related Articles

How to make a kivy label multiline text?

View Discussion
Improve Article
Save Article
  • Last Updated : 26 Sep, 2021

Kivy is an open source software library for the rapid development of applications equipped with novel user interfaces, such as multi-touch apps. Using Kivy on your computer, you can create apps that run on:

  • Desktop computers: OS X, Linux, Windows.
  • IOS devices: iPad, iPhone.
  • Android devices: tablets, phones.
  • Any other touch-enabled professional/home brew devices supporting TUIO (Tangible User Interface Objects).

Label in Kivy

The Label widget is for rendering text. It supports ascii and unicode strings. Label is the text which we want to add on 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.

Procedure 

  1. Install kivy on your pc using cmd command “pip install kivy”
  2. Import kivy and its App module as shown in the example below
  3. Create a class that inherits the App module
  4. Define a build method in the class and define the label you want to create in this method and then return the label
  5. Create an object for the class
  6. Lastly use run() command for the object

Normal Label Code 

Python3




# make sure you have installed kivy for this to work
import kivy
 
# base Class of your App inherits from the App class.
# app:always refers to the instance of your application
from kivy.app import App
 
# if you not import label and use it it through error
from kivy.uix.label import Label
 
# defining the App class
 
class MyDemoApp(App):
    def build(self):
        # label display the text on screen
        ll = Label(text="This is a normal label")
        return ll
 
# creating the object
label = MyDemoApp()
# run the window
label.run()

Output :

Multiline Label Code 

You can easily make the text multiline using ‘\n’ at the end of each line.

Example 1 :

Python3




import kivy
from kivy.app import App
from kivy.uix.label import Label
 
class MyDemoApp(App):
    def build(self):
        ll = Label(text="This is a\nmultiline\nlabel")
        return ll
 
label = MyDemoApp()
 
label.run()

Output :

Example 2 :

Python3




import kivy
from kivy.app import App
from kivy.uix.label import Label
 
class MyDemoApp(App):
    def build(self):
        ll = Label(text="GeeksForGeeks is the \nbest platform for \nDSA content")
        return ll
 
label = MyDemoApp()
 
label.run()

Output :

Example 3 :

Python3




import kivy
from kivy.app import App
from kivy.uix.label import Label
 
class MyDemoApp(App):
    def build(self):
        ll = Label(text='''Kivy is an open source software library 
                          \nfor the rapid development of applications 
                          \nequipped with novel user interfaces, 
                          \nsuch as multi-touch apps.''')
        return ll
 
label = MyDemoApp()
 
label.run()

Output :

Note: For more information on kivy you can visit official docs using this link.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!