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
- Install kivy on your pc using cmd command “pip install kivy”
- Import kivy and its App module as shown in the example below
- Create a class that inherits the App module
- Define a build method in the class and define the label you want to create in this method and then return the label
- Create an object for the class
- 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 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 = "w3wiki 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.
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:
- 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).