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

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: