Elon Musk’s Mars colonization event in 5 minutes

Ice wave Lake Baikal, Russia

Kivy For The Non Computer Scientist – Part 9: Dynamic Checkbox Labels

If you’ve been following this series of Kivy blog posts this may be where you want to review what I did to make the checkbox dynamic and then review and try the code on the official Kivy CheckBox page as they differ.

My code below builds upon the code shown in Part 8 of this blog series and the widgets on_press method to call a standard Python function. That function makes the checkbox dynamic by changing the feedback_label’s text to “Hello, world!” when checked and “Goodbye, world!” when unchecked. Since Kivy runs in a continuous loop on can check and uncheck the check box as much as they want and the feedback_label text will change dynamically.

The code to make the checkbox dynamic is as follows;

dynamic


import kivy # Required to run Kivy such as the next line of code
kivy.require('1.9.1') # used to alert user if this code is run on an earlier version of Kivy.
from kivy.app import App # Imports the base App class required for Kivy Apps
from kivy.uix.gridlayout import GridLayout # imports the GridLayout class for use in the app.
from kivy.uix.label import Label # Imports the Label Screen Widget for use in the app.
from kivy.uix.checkbox import CheckBox # Imports the Check Box Screen Widget for use in the app.
class ThreeColumnApp(App): # Creates the instance (copy) of the Kivy App class named ThreeColumnApp.
    def build(self): # build is a method of Kivy's App class used to place widgets onto the GUI.
        my_layout = GridLayout(cols = 3) # Sets grid layout and the number of columns to 3 as the my_layout instance is created.
        self.my_check_box = CheckBox(on_press = self.hello_world) # Creates a Check Box instance and sets its text property to I'm A Button. Press Me.
        self.my_check_box_label = Label(text="Please Check Me") # Creates a Label Widget instance and sets its text property to Please Press My Button.
        self.my_feedback_label = Label(text="I'm Your Feedback Label") # Creates a Label Widget instance and sets its text property to Im Your Feedback Label.
        my_layout.add_widget(self.my_check_box) # Adds the check box widget to my_layout
        my_layout.add_widget(self.my_check_box_label) # Adds the label widget to My_layout
        my_layout.add_widget(self.my_feedback_label) # Adds the label widget to My_layout
        return my_layout # return calls the build method which in turn builds the GUI.
    def hello_world(self,event): # Creates the hello_world function to be used by checkbox as its event.
        if self.my_feedback_label.text != "Hello, world!": # Tests to see if Hello, world is current label text.
        self.my_feedback_label.text = "Hello, world!" # Sets the GUIs label text property to Hello, world!.
    else:
        self.my_feedback_label.text = "Goodbye, world!" # Sets the GUIs label text property to Goodbye, world!.
ThreeColumnApp().run() # Calls the ThreeColumnApp object to run the app.

  • Lines 17 thru 21 – The only change to this code from Part 8 is in the hello_world function where a Python if … else statement has been written. Simply put the If statement determines if the feedback_labels text is “Hello, world!” when called. If it is “Hello, world!” the function changes the feedback_labels text to “Goodbye, world!”.
    • If the function finds the feedback_label not equal to “Hello, world!” it changes it to “Hello, world!” which is great for the first check and all subsequent checks to the checkbox.

Again, I invite you to look at the method associated with the CheckBox widget found in Kivy’s documentation. Its different in that the method issues a boolean when the checkbox is clicked. I simply struggled in getting it to work because the page did not provide a full working version of its code.

I like the the code I provided above because it allows me to write Python code in the manner I’m used to and via a function one check can be programmed to do many things. We’ve now learned to dynamicize Kivy’s buttons and checkboxes in 22 lines of Python code. And the fact that we can now trigger our code with either a checkbox of a button and dynamically update our screen we now have the basics we need to produce apps employing a Kivy GUI.

….brad….

Kivy For The Non Computer Scientist – Part 8: Adding A Checkbox Label

Unfortunately with Kivy the Checkbox screen widget (at the time of writing) does not come with a text property that would create a checkbox label. Since advanced layouts at this point in my Kivy experience remain a bit of a mystery, I came up with a solution by adding a third column that would be used as a label for the checkbox.

The code is as follows;

check

import kivy # Required to run Kivy such as the next line of code
kivy.require('1.9.1') # used to alert user if this code is run on an earlier version of Kivy.
from kivy.app import App # Imports the base App class required for Kivy Apps
from kivy.uix.gridlayout import GridLayout # imports the GridLayout class for use in the app.
from kivy.uix.label import Label # Imports the Label Screen Widget for use in the app.
from kivy.uix.checkbox import CheckBox # Imports the Check Box Screen Widget for use in the app.
class ThreeColumnApp(App): # Creates the instance (copy) of the Kivy App class named ThreeColumnApp.
    def build(self): # build is a method of Kivy's App class used to place widgets onto the GUI.
        my_layout = GridLayout(cols = 3) # Sets grid layout and the number of columns to 3 as the my_layout instance is created.
        self.my_check_box = CheckBox(on_press = self.hello_world) # Creates a Check Box instance and sets its text property to I'm A Button. Press Me.
        self.my_check_box_label = Label(text="Please Check Me") # Creates a Label Widget instance and sets its text property to Please Press My Button.
        self.my_feedback_label = Label(text="I'm Your Feedback Label") # Creates a Label Widget instance and sets its text property to Im Your Feedback Label.
        my_layout.add_widget(self.my_check_box) # Adds the check box widget to my_layout
        my_layout.add_widget(self.my_check_box_label) # Adds the label widget to My_layout
        my_layout.add_widget(self.my_feedback_label) # Adds the label widget to My_layout
        return my_layout # return calls the build method which in turn builds the GUI.
    def hello_world(self,event): # Creates the hello_world function to be used by checkbox as its event.
        self.my_feedback_label.text = "Hello, world!" # Sets the GUIs feedback_label text property to Hello, world! when function is called.
ThreeColumnApp().run() # Calls the ThreeColumnApp object to run the app.

The code has a few changes in it from that used in Part 7 of this blog series but no new concepts were introduced with this code;

  • Line 9 sets this app up as a 3 column layout.
    • The class name (Line 7) is changed to reflect its three column design.
  • Lines 10, 11 and 12 establish instances of the three screen widgets for use in the app.
    • Line 11 is the additional label widget that’s used as the checkbox label.
    • Line 12 is identified as the feedback label that will change when the checkbox is clicked
  • Line 18 changes the feedback text when the hello_world function is called.

While the interface isn’t pretty at this point but it is at least functional using 19 lines of code. You should by now have a pretty good handle on what each line of code does. My next post will add a couple of lines of code that will update the screen when the checkbox is checked or  unchecked.

….brad….

Here is the link to the next post in this blog series: Kivy For The Non Computer Scientist – Part 9: Dynamic Checkbox Labels

Kivy For The Non Computer Scientist – Part 7: Simple Checkbox Code

By now (hopefully) you are gaining some confidence with Python code that incorporates Kivy. The code below integrates a simple Check Box that when “checked” changes the label to “Hello, world!”

checkbox

Here is the editable source,


import kivy # Required to run Kivy such as the next line of code
kivy.require('1.9.1') # used to alert user if this code is run on an earlier version of Kivy.
from kivy.app import App # Imports the base App class required for Kivy Apps
from kivy.uix.gridlayout import GridLayout # imports the GridLayout class for use in the app.
from kivy.uix.label import Label # Imports the Label Screen Widget for use in the app.
from kivy.uix.checkbox import CheckBox # Imports the Check Box Screen Widget for use in the app.
class TwoColumnApp(App): # Creates the instance (copy) of the Kivy App class named TwoColumnApp.
    def build(self): # build is a method of Kivy's App class used to place widgets onto the GUI.
        my_layout = GridLayout(cols = 2) # Sets grid layout and the number of columns to my_layout as the instance is created. One less line of code.
        self.my_first_check_box = CheckBox(on_press = self.hello_world) # Creates a Check Box instance and sets its text property to I'm A Button. Press Me.
        self.my_first_label = Label(text="Please Check Me") # Creates a Label Widget instance and sets its text property to Please Press My Button.
        my_layout.add_widget(self.my_first_check_box) # Adds the check box widget to my_layout
        my_layout.add_widget(self.my_first_label) # Adds the label widget to My_layout
        return my_layout # return calls the build method which in turn builds the GUI.
    def hello_world(self,event): # Creates the hello_world function to be used by checkbox as its event.
        self.my_first_label.text = "Hello, world!" # Sets the GUIs label text property to Hello, world! when function is called.
TwoColumnApp().run() # Calls the TwoColumnApp object to run the app.

So What’s Different?

From the button code introduced in Part 6 of this blog series the changes are as follows,

  • Line 6 imports the Check Box screen widget for use in the app.
  • Line 10 creates a Check Box instance titled self.my_first_check_box. Check Box still has the on_press method that calls the hello_world function as part of its code base but it differs from a Button because it does not have a text property.
  • Line 12 adds the self.my_first_check_box Check Box to my_layout.

Now that we have the label working, this application begs two more questions;

  1. How do I label a Check Box?
  2. How do I make an action occur when the Check Box is not “clicked”?

We will look into these questions in the next two posts.

….brad….

Here is the link to the next post in this series: Kivy For The Non Computer Scientist – Part 8: Adding A Checkbox Label

Kivy For The Non Computer Scientist – Part 6: Code Cleanup And Kivy Convention.

Before moving on I’d be remiss if I didn’t create a short post and update the code from Part 5 of this blog post series to make it more Pythonic and to conform to Kivy convention(s). Here is the updated code;


import kivy # Required to run Kivy such as the next line of code
kivy.require('1.9.1') # used to alert user if this code is run on an earlier version of Kivy.
from kivy.app import App # Imports the base App class required for Kivy Apps
from kivy.uix.gridlayout import GridLayout # imports the GridLayout class for use in the app.
from kivy.uix.label import Label # Imports the Label Screen Widget for use in the app.
from kivy.uix.button import Button # Imports the Button Screen Widget for use in the app.
class TwoColumnApp(App): # Creates the instance (copy) of the Kivy App class named TwoColumnApp.
    def build(self): # build is a method of Kivy's App class used to place widgets onto the GUI.
        my_layout = GridLayout(cols = 2) # Sets grid layout and the number of columns to my_layout as the instance is created. One less line of code.
        self.my_first_button = Button(text="I'm A Button. Press Me", on_press = self.hello_world) # Creates a Button Widget instance and sets its text property to I'm A Button. Press Me.
        self.my_first_label = Label(text="Please Press My Button") # Creates a Label Widget instance and sets its text property to Please Press My Button.
        my_layout.add_widget(self.my_first_button) # Adds the button widget to my_layout
        my_layout.add_widget(self.my_first_label) # Adds the label widget to My_layout
        return my_layout # return calls the build method which in turn builds the GUI.
    def hello_world(self,event): # Creates the hello_world function to be used by button as its event.
        self.my_first_label.text = "Hello, world!" # Sets the GUIs label text property to Hello, world! when function is called.
TwoColumnApp().run() # Calls the TwoColumnApp object to run the app.

What’s Added?

Line 1 – Up to this point in our code creation we have only used widgets associated with Kivy’s App class. However in order to use other elements of Kivy, such as .kv language or in this case Kivy’s version requirement method on Line 2, one starts with the import kivy statement kivy because it loads Kivy’s framework.

Line 2 is code that uses Kivy’s framework to check the minimum version required to run this Kivy application. The programmer sets it to the Kivy version on which the code is written. If a user attempts to run your application with a version of Kivy that is older than the specified version, an Exception is raised.

Line 9 concatenates what was two lines of code into one by setting my_layout’s column count by setting it using GridLayout’s brackets.

Next In The Series

Since the learning goal of this series was to build a button, switch and checkbox interface, the next blog post will focus on the checkbox.

….brad….

Next post in this blog series: Kivy For The Non Computer Scientist – Part 7: Simple Checkbox Code

Kivy For The Non Computer Scientist – Part 5: Making A Kivy Button Do Something

As stated in Part 2 the goal of this blog series is to produce a simple button, switch and checkbox interface with very simple and complete code examples. While Part 3 provided the background and code related to Kivys Object Oriented Programming (OOP) principles required by Kivy and Part 4 provided the code on simple screen layouts, this blog entry will build upon the code from Part 4 by providing the code to change the text fn the label on the GUI to Hello. world! by mouse clicking or touching the button. The complete code to accomplish this is as follows;

pressgui

The code is as follows (from Part 4 with Button Event code added);


from kivy.app import App # Imports the base App class required for Kivy Apps
from kivy.uix.gridlayout import GridLayout # imports the GridLayout class for use in the app.
from kivy.uix.label import Label # Imports the Label Screen Widget for use in the app.
from kivy.uix.button import Button # Imports the Button Screen Widget for use in the app.
class TwoColumnApp(App): # Creates the instance (copy) of the Kivy App class named TwoColumnApp.
    def build(self): # build is a method of Kivy's App class used to place widgets onto the GUI.
        my_layout = GridLayout() # Creates a GridLayout titled my_layout for use in the app.
        my_layout.cols = 2 # Sets my_layout column property to 2
        self.my_first_button = Button(text="I'm A Button. Press Me", on_press = self.hello_world) # Creates a Button Widget instance and sets its text property to I'm A Button. Press Me.
        self.my_first_label = Label(text="Please Press My Button") # Creates a Label Widget instance and sets its text property to Please Press My Button.
        my_layout.add_widget(self.my_first_button) # Adds the button widget to my_layout
        my_layout.add_widget(self.my_first_label) # Adds the label widget to My_layout
        return my_layout # return calls the build method which in turn builds the GUI.
    def hello_world(self,event):
        self.my_first_label.text = "Hello, world!"
TwoColumnApp().run() # Calls the TwoColumnApp object to run the app.

So Where Is The Code That Makes The Kivy Button Trigger An Event?

The additional code to create the button event is as follows;

  • On Lines 14 and 15 an event function is named and its action defined.
    • Line 14 names the function hello_world.
    • Line 15 instructs the self.my_first_label to change its text to Hello, world!
  • Line 9 adds on_press = self.hello_world to the self.my_first_button code.
    • It is the buttons on_press behavior method that calls an event. In this case it is set to self.hello_world which calls the hello_world function. As shown above the hello_world function changes Kivy’s Label Widget text to Hello, world!.

Here is the final code for Part 5 with each line commented explaining what it does;


from kivy.app import App # Imports the base App class required for Kivy Apps
from kivy.uix.gridlayout import GridLayout # imports the GridLayout class for use in the app.
from kivy.uix.label import Label # Imports the Label Screen Widget for use in the app.
from kivy.uix.button import Button # Imports the Button Screen Widget for use in the app.
class TwoColumnApp(App): # Creates the instance (copy) of the Kivy App class named TwoColumnApp.
    def build(self): # build is a method of Kivy's App class used to place widgets onto the GUI.
        my_layout = GridLayout() # Creates a GridLayout titled my_layout for use in the app.
        my_layout.cols = 2 # Sets my_layout column property to 2
        self.my_first_button = Button(text="I'm A Button. Press Me", on_press = self.hello_world) # Creates a Button Widget instance and sets its text property to I'm A Button. Press Me.
        self.my_first_label = Label(text="Please Press My Button") # Creates a Label Widget instance and sets its text property to Please Press My Button.
        my_layout.add_widget(self.my_first_button) # Adds the button widget to my_layout
        my_layout.add_widget(self.my_first_label) # Adds the label widget to My_layout
        return my_layout # return calls the build method which in turn builds the GUI.
    def hello_world(self,event): # Creates the hello_world function to be used by button as its event.
        self.my_first_label.text = "Hello, world!" # Sets the GUIs label text property to Hello, world! when function is called.
TwoColumnApp().run() # Calls the TwoColumnApp object to run the app.

With these first five blogposts you should now be on your way to start creating your own KIVY interface. You now know the basics of how Kivy’s OOP, screen layouts, screen widgets and button event handling works. You now have a simple straightforward code base (only 16 lines of code) that will help you get started on your own Kivy GUI that uses button and label widgets.

….brad….

Link to the next blog post in this series: Kivy For The Non Computer Scientist – Part 6: Code Cleanup And Kivy Convention.