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!”


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 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.


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


Leave a Reply

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

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

Google photo

You are commenting using your Google 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

%d bloggers like this: