Skip to content
Related Articles

Related Articles

Improve Article

Python | Create checkbox using .kv file

  • Last Updated : 07 Jul, 2021

Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, Linux, and Windows etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications.

Kivy Tutorial – Learn Kivy with Examples.

Checkbox widget: 
CheckBox is a specific two-state button that can be either checked or unchecked.
To work with the Checkbox you first have to import Checkbox from the module which consists all features, functions of the checkbox i.e. 

from kivy.uix.checkbox import CheckBox 
Basic Approach to follow while creating Checkbox using .kv file :
1) import kivy
2) import kivyApp
3) import BoxLayout
4) import Checkbox
5) set minimum version(optional)
6) Extend the container class
7) set up .kv file :
9) Return layout
10) Run an instance of the class

Now the program of How to create Checkbox in Kivy using .kv file:


# file
# program for creating checkbox using .kv in kivy.
# import kivy module
import kivy
# set require version
# base Class of your App inherits from the App class. 
# app:always refers to the instance of your application 
from import App
from kivy.uix.boxlayout import BoxLayout
## not necessary while using .kv file
from kivy.uix.checkbox import CheckBox
# To do some manupulation on window impoet window
from kivy.core.window import Window
# Container class for the app's widgets
class SampBoxLayout(BoxLayout):
    # Callback for the checkbox
    def checkbox_click(self, instance, value):
        if value is True:
            print("Checkbox Checked")
            print("Checkbox Unchecked")
# App derived from App class
class SampleApp(App):
    # build is a method of Kivy's App class used
    # to place widgets onto the GUI.
    def build(self):
        # setting up window background color
        Window.clearcolor = (0, 0, .30, .60)
        return SampBoxLayout()
# Rum the app
root = SampleApp()

sample.kv file of the code. 


#.kv file of file
#: import CheckBox kivy.uix.checkbox
# giving colour to lable
    color: .761, .190, .810, 1
    orientation: "vertical"
    padding: 10
    spacing: 10
        text: "Gender"
        size_hint_x: 1
    # creating box layout
        # assigning orentation
        orientation: "horizontal"
        height: 20
            orientation: "horizontal"
            size_hint_x: .22
            # label creation
                text: "Male"
                size_hint_x: .80
                color:.294, .761, .623
                on_active: root.checkbox_click(self,
                size_hint_x: .20
                text: "Female"
                size_hint_x: .80
                on_active: root.checkbox_click(self,
                size_hint_x: .20
                text: "Other"
                size_hint_x: .80
                on_active: root.checkbox_click(self,
                size_hint_x: .20


Video output: 


 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :