Kivy For The Non Computer Scientist – Part 2: Concepts To Get A Handle On

My learning goal around Kivy was to produce a simple button, switch and checkbox interface. From my previous work with Tkinter I had a basic understanding of what screen widgets are. Screen Widgets are the pre-made buttons, labels and checkboxes etc. that make up a standard interface. For me my initial barriers in applying Kivy were in gaining a practical understanding of how the widgets Object Oriented Programming (OOP) worked and how Kivy’s screen layouts worked. Turned out the basic knowledge relating to working with screen layouts and widgets is OOP, so having a basic understanding of OOP is an essential component to work with Kivy.

Object Oriented Programming

In an interview for Rolling Stone magazine in 1994, Steve Jobs was asked about Object Oriented Programming. He replied as follows: ” Objects are like people. They’re living, breathing things that have knowledge inside them about how to do things and have memory inside them so they can remember things. And rather than interacting with them at a very low level, you interact with them at a very high level of abstraction.”

I find the best way to describe objects as code that somebody has already written for you. When you use an object you can alter it’s properties and employ its methods to make it useful for your particular application.

ben

 

Consider the image above. In programming nomenclature, the image shows a dog object. The object (or dog) is named Ben. Ben comes with several physical properties (height, weight, colour etc.) that as a programmer I can change and customize for my application. As well Ben comes with several methods such as sit and lay down. It’s pretty clear what these methods do and I can call these methods at any time in my code depending on what I want Ben to do.

The point of the Object is that I –as the programmer– didn’t have to write any of the low level code that ensures the height and weight work or any low level code to make Ben beg or sit. I as the programmer only have to customize the object via its properties and call its methods as needed. Hence the term Object Oriented Programming and the origin of Steve Jobs assertion that one interacts with an object at a much higher level.

If you’re a Python programmer you already understand that in order to get specialized objects (such as Kivy and its screen widgets) you first must add the library to your Python installation. This is usually done using PIP and the installation procedure is well documented on the kivy.org website.

instance

To both make and use an object one must first create an instance (ie. a copy) of it via ones computer code. One can then alter the properties of the object and call any methods as required. Note in its original form the object is referred to as a class. Only when an instance of it is made is it referred to as an object. One can use an object as many times as they wish by creating additional instances of it usually with a different name. When several instances of an object are created, each different object can be garnished with unique properties –such as colour and size in the above example– and methods will only occur when the specific objects method is called.

The Use Of Objects In Kivy

kivy-gpio-raspberry-pi-touch-thumb

Given the above explanation of OOP it’s not hard to understand that each of Kivy’s screen widgets are invoked as objects. If you watched Matt Richardson’s video in Part 1 of this Kivy training blog, you can plainly see buttons and the slider screen widgets he employed when developing his GUI. You would probably understand that the button widget and slider widget are different widgets and might have different properties and methods. As well his GUI implementation employs three different instances of the same button class used to control the different hardware elements installed on his breadboard.

Kivy’s Screen Layouts Are Also Objects And Nested

Currently Kivi has eight different screen layouts. If you’re new to Kivy I would avoid reading about them at this time as the documentation is detailed and once you learn how to invoke them you’ll require the detail. As mentioned above the screen layouts too are invoked as objects. Their purpose is to provide the base on which one places the appropriate GUI screen widgets. The reason that there are different screen layouts is because the properties differ from layout to layout on how widgets are positioned.

Here is the key thing to know about Kivy’s screen layouts. The reason it took so long for the “penny to drop” on how Kivy’s GUI’s are actually constructed for me was because a simple Kivy interface may actually contain several of these screen layouts. In other words its common practice that several different screen layouts may be nested in order to achieve the desired widget positioning.

Like the OOP concepts noted above, nested Kivy screen layouts are important concept to understand right from the start. It makes it a lot easier to understand the Python code when you read it. In my next blog post I’ll introduce some simple code that will help you grasp the concepts outlined in this post as quickly as possible.

….brad….

Next post in this blog series: Kivy For The Non Computer Scientist – Part 3: Kivy’s Hello, World! Examined

Advertisements

2 Responses

  1. […] Here is the link to Kivy For The Non Computer Scientist – Part 2: Concepts To Get A Handle On […]

  2. […] mentioned in Kivy For The Non Computer Scientist – Part 2: Concepts To Get A Handle On, the second blog post in this series, I mentioned one of key Kivy concepts is that more complex […]

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: