Table of contents
1.
Introduction
2.
Widgets and Labels
3.
Creating a label
4.
Decorating a label widget
5.
Frequently Asked Questions
5.1.
What do you know about Kivy?
5.2.
What are widgets in Kivy?   
5.3.
What is a Label widget in Kivy?
5.4.
How can we change the size of the text in a label?
5.5.
How to add multiple lines to a label?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Label Widget in Kivy

Author Jaglike Makkar
2 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Kivy is an open-source Python toolkit for quickly developing applications with novel user interfaces, like multi-touch apps. It is mostly used to create Android applications, but it may also be used to create desktop applications. In this blog, we'll look at how to add labels to Kivy-created windows.

Widgets and Labels

The goal behind widgets is to have a collection of different types of elements that we can combine to make an application. Consider a login page on a home page. You may have text on your home page that says things like "username" and "password." After that, you'll need some text fields for input. To do this using Kivy, we'll use a Label widget to hold the content and then a text input widget to allow the user to type something.

Creating a label

The Label widget is used to display text. It can handle both ASCII and Unicode text. It is the text that we want to put on our windows, buttons, and other elements. We can also add styling to labels, such as increasing the text size, color, etc.

In the example below, we construct a window and use the Label method from the uix.label module to assign it a customized label. A new window appears with the custom label when we start the app with this code.

from kivy.app import App
from kivy.uix.label import Label

class NewLabel(App):
   def build(self):
      # Label text to be edited
      label = Label(text ="Hello Label Text ! ")
      return label

NewLabel().run()
You can also try this code with Online Python Compiler
Run Code

 

Output:

When we run the code above, we get the following result:

Decorating a label widget

Additional arguments available as part of the Label widget function can be used to change the colour and font size of the label.

from kivy.app import App
from kivy.uix.label import Label

class MyApplication(App):
   def build(self):
      # Label text to be edited
      label = Label(text ="Hello World!! \nThis is a decorated label",font_size='30sp',color = [0.56, 0.56, 0.24, 1])
      return label
      
MyApplication().run()
You can also try this code with Online Python Compiler
Run Code

 

Output:

Frequently Asked Questions

What do you know about Kivy?

Kivy is an open-source Python toolkit for quickly developing applications with novel user interfaces, like multi-touch apps. It is mostly used to create Android applications, but it may also be used to create desktop applications.

What are widgets in Kivy?   

Widgets are a collection of different types of elements that we can combine to make an application.

What is a Label widget in Kivy?

The Label widget is used to display text. It can handle both ASCII and Unicode text. It is the text that we want to put on our windows, buttons, and other elements.

How can we change the size of the text in a label?

To change the size, use text size to confine the text and/or bind size to texture size to let the text to grow with it.

How to add multiple lines to a label?

Using 'n' at the end of a line, you can easily make the text multiline.

Conclusion

In this article, we studied in detail label widgets in Kivy. We saw how to create and decorate a label widget and examples. We hope that this blog has helped you enhance your knowledge of the label widget. Do upvote our blog to help other ninjas grow.

After reading about the Label Widget, are you not feeling excited to read/explore more articles on the topic of KIVY? Don't worry; Coding Ninjas has you covered. To learn, see the Introduction to KivyThe drop-down List In KivyWidgets in Kivy, and Kivy Float Layout in Python.

Refer to our Guided Paths on Coding Ninjas Studio to upskill yourself in DSACompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But suppose you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problems, interview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass