Table of contents
1.
Introduction
2.
Creating a Checkbox
2.1.
Output
3.
Frequently Asked Questions
3.1.
What is bind()?
3.2.
What is kivy.uix?
3.3.
What is .kv file?
3.4.
What does root mean in Kivy?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Checkbox using .kv file

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

We all have come across checkboxes on multiple websites. Checkboxes are specified two-state button that toggles between two states: checked or unchecked. This behavior is helpful in many scenarios making checkboxes one of the essential elements in any webpage.

Kivy is a platform-independent GUI tool in python that lets us run the code in Android, IOS, Linux, and Windows. Thus to create applications across different platforms, it would be great to use kivy. Therefore, combining the two most essential requirements, we come across how we can create a checkbox using the .kv file.

Thus, in this blog, we will learn how to create checkboxes in the .kv file.

Creating a Checkbox

With any other UI element, we also need to import the Checkbox from the module before we can start making a checkbox. First, we need to create the container class for the Checkbox.

import kivy
kivy.require("1.9.0")

# these are the imports we will need in the blog
from kivy.app import App


#imports the box layout
from kivy.uix.boxlayout import BoxLayout


#imports checkbox widget
from kivy.uix.checkbox import Checkbox

from kivy.core.window import Window
class testBox(BoxLayout):
     #here we are defining the function that will be called when there is action in the checkbox
     #the function here takes in the value i.e. the current status of the checkbox and prints the output accordingly. Here value is boolean
def checkbox_click(self, instance, value):
if value is True:
print("Checked")
else:
print("Unchecked")
class checkboxApp(App):
     #here we are building the main component of our project
def build(self):
           #we are defining the color of the window
Window.clearcolor=(0, 0, .30, .60)
return SampBoxLayout()
root = checkboxApp()
root.run()

Now we need to create the checkbox.kv file to make the Checkbox layout. The name of the .kv file should be the name of the function that we run in root i.e. checkboxApp here as any other name as the kv file will not include it in the run.

The below function is basically how we define the whole display of the GUI we are creating in a nested format where the nest basically defines one element as a parent and the other as a child element.

<testBox>:
      #defines the orientation of the child elements
orientation: "vertical"

CustLabel:
            #defines the content inside the label and the font size
text: "Profession"
font_size: 15

BoxLayout:
orientation: "horizontal"
height: 15

CustLabel:
text: "Student"
font_size: 12
CheckBox:
                  #defines which function to call when there is some action in the checkbox and passes the status of the checkbox as an argument
on_active: root.checkbox_click(self, self.active)

CustLabel:
text: "Other"
font_size: 12
CheckBox:
on_active: root.checkbox_click(self, self.active)

Now we can create a checkbox using the .kv file.

Output

Frequently Asked Questions

What is bind()?

Ans: It is a function that binds a callback to a particular event on an element. It takes the input of two parameters, the callback and the event.
 

What is kivy.uix?

Ans: kivy.uix is a module that contains classes for creating and managing Widgets for creation in any Application.

 

What is .kv file?

Ans: A .kv file helps create a widget tree and the explicit declaration of bindings.

 

What does root mean in Kivy?

Ans: In a kv file the root always refers to a parent with angle brackets and thus there can be multiple roots. 

Conclusion

This blog discussed how we could create Checkbox and bind functions to them using Kivy.

You may want to learn more about Kivy here, float layout in kivy here and Button Action in Kivy here. We hope that this blog has helped you enhance your knowledge regarding creating a checkbox using a .kv file. Do upvote our blog to help other ninjas grow.


Learning never stops, and to feed your quest to learn and become more skilled, head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, take on guided paths, read interview experiences, try our interview bundle and much more.!

Happy Learning!

Live masterclass