Open In App

How to make a kivy label multiline text?

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:

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 






# 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 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 :




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 :




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 :




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.


Article Tags :