In this Python Kivy article we want to learn How to Create Label in Python Kivy, label is a basic user interface element used to display text in Kivy app. Labels can be used to display headings, instructions or any other text content that your app requires. in this article we want to talk that how to create a label in Kivy.
For creating Label in Kivy, first of all we need to import required classes from Kivy module, in this code, we have imported Kivy framework and Label widget.
1 2 3 |
import kivy from kivy.app import App from kivy.uix.label import Label |
After that we have imported necessary Kivy modules, next step is to create a Label widget, in this code, we have created a class called MyLabelApp that extends from the App class. after that we have defined build() method for the class that creates new Label widget.
1 2 3 4 5 6 |
class MyLabelApp(App): def build(self): # Create Label widget label = Label(text="Kivy label - geekscoders.com") return label |
And finally we need to run our Python Kivy application.
1 2 |
if __name__ == '__main__': MyLabelApp().run() |
This is the complete code for this article
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import kivy from kivy.app import App from kivy.uix.label import Label class MyLabelApp(App): def build(self): # Create Label widget label = Label(text="Kivy label - geekscoders.com") return label if __name__ == '__main__': MyLabelApp().run() |
Run the code and this will be the result
Also you can change the color and font size of the Python Kivy Label like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import kivy from kivy.app import App from kivy.uix.label import Label class MyLabelApp(App): def build(self): # Create Label widget label = Label(text="Kivy label - geekscoders.com") label.font_size = 40 label.color = "yellow" return label if __name__ == '__main__': MyLabelApp().run() |
Run the code and this will be the result