Kivy First program

This blog demonstrate how to print ‘Hello World’ in the Label widget

First of all, import kivy and specify the version to be used

import kivy
kivy.require('1.0.6')

Define App class which is the base class

from kivy.app import App

Specify the widget that require in our program. We require only Label widget. uix is the module which contain user interfaces such as layout and widgets

from kivy.uix.label import Label

Now, we will define sub class. App is the base class for MyApp subclass

class MyApp(App):

Under sub class, function build() is define which initialize and returns root widget. Below Label is the root widget. Label is initialized with text ‘Hello World’ and a instance of Label is return

def build(self):
return Label(text='Hello world')

Below statement start kivy application. class MyApp is initialized and the run() method is called

if __name__ == '__main__':
MyApp().run()

Output

Complete Code

import kivy
kivy.require('1.0.6') # replace with your current kivy version !

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


class MyApp(App):

def build(self):
return Label(text='Hello world')


if __name__ == '__main__':
MyApp().run()
Advertisement

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s