Kivy Program : Displaying Button widget with text ‘Hello World’

This blog creates a program that display Button widget on the screen with text ‘Hello World’

In the previous blog, we have created a program that displays Label widget with text ‘Hello World’. 99% percent of the program will be same with minor changes regarding importing widget. Here we will import Button widget instead of Label widget

Import Kivy module and define kivy version

import kivy
kivy.require('1.0.6')

Define App class. This is the base class

from kivy.app import App

Specify widget that require in program. Importing Button widget.

from kivy.uix.button import Button

Create a sub class. MyApp is the sub class which inherits properties from base class App

class MyApp(App):

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

def build(self):
return Button(text='Hello World')

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

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

Output

Run the program and click on the button

Complete Code

import kivy
kivy.require('1.0.6')

from kivy.app import App
from kivy.uix.button import Button

class MyApp(App):
def build(self):
return Button(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