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.

Here is the link to Kivy For The Non Computer Scientist – Part 10: KV Language the next post in this series.

….brad….

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: