Kivy For The Non Computer Scientist – Part 3: Kivy’s Hello, World! Examined

As mentioned in Part 1 of this blog series, kivy.org’s Hello, world! app is a good study as it’s written. It is a simple and complete piece of code that helps one build upon the principles found in Part 2 of this blog series. Kivy’s Hello World code is listed as follows;

29254863550_ce8cba560e_o

Its Python code is as follows;

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

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

TestApp().run()

The Hello, world! app is written to reflect Kivy’s Object Oriented Programming (OOP) principles that are outlined in Part 2 of this blog series. Remember, if your new to it, objects are pieces of code that someone else has written for you. One interacts with objects by creating an instance of it (copying it) and then customizing its properties or calling its methods for use in your application.

Practically applied these OOP principles start with the first line of code in the app;

from kivy.app import App # Imports the base App class required for Kivy Apps
from kivy.uix.button import Button

class TestApp(App): # Creates the instance (copy) of the Kivy App class named TestApp
    def build(self):
        return Button(text='Hello World')

TestApp().run()

To create the “Hello World” text and button;

  • Line 2 of the code imports Kivy’s Button Widget for use in the app.
  • Line 6 of the code creates an instance of Kivy’s Button Widget with the buttons text property set to “Hello World”.
from kivy.app import App
from kivy.uix.button import Button # Imports the Button Widget for use in this app.

class TestApp(App):
    def build(self):
        return Button(text='Hello World') # Creates a Button Widget instance and sets it text property to "Hello World".

TestApp().run()

To place the “Hello World” button on the Kivy GUI;

  • Line 5 references the build method. build is a method of Kivy’s App class and was inherited by the TestApp object when it was created. build is an object that simply places screen widgets (in this case the button) on the GUI when called.
  • Line 6 starts with the command return. When the program is run, return calls the build method which in turn places the button with the “Hello World” text onto the GUI.
from kivy.app import App
from kivy.uix.button import Button

class TestApp(App):
    def build(self): # build is a method of Kivy's App class used to place widgets onto the GUI.
        return Button(text='Hello World') # return calls the build method which places button on GUI.

TestApp().run()

To start the Kivy GUI;

  • Line 8 calls the TestApp object to run.
  • It should be noted that when a Kivy app runs, it runs in a continuous loop until the the program is terminated.

Lessons Learned

To conclude one can see within the six lines of simple Python code that makes up Kivy’s “Hello World” application the magic of Object Oriented Programming (OOP). Within those six lines of code and as outlined in Part 2 of this blog series, one creates two instances of different Kivy classes and employs them as objects by either calling their methods or by customizing their properties. No where in the code did I –as the programmer– have to write the low level code that makes the basic Kivy GUI or the “Hello World” button operate. We’ll soon reach the point where we will want to make a button actually do something. But before we do that we’ll touch simply and quickly on Screen Layouts in the next blog post.

Below is the complete code for the Kivy “Hello World” app complete with comments on each line should you wish to play with the code and/or build upon it.

from kivy.app import App # Imports the base App class required for Kivy Apps
from kivy.uix.button import Button # Imports the Button Widget for use in this app.

class TestApp(App): # Creates the instance (copy) of the Kivy App class named TestApp
    def build(self): # build is a method of Kivy's App class used to place widgets onto the GUI.
        return Button(text='Hello World') # Creates a Button Widget instance and sets it text property to Hello World

TestApp().run() # Calls the TestApp object to run the app.

….brad….

Next blog post in this series: Kivy For The Non Computer Scientist – Part 4: A Little About Kivy’s Screen Layouts

Advertisements

Leave a Reply

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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: