Python | Create checkbox using .kv file

Widget flag:
CheckBox — it is a special two-state button that can be either checked or unchecked.

To work with a checkbox, you first need to import the checkbox from the module, which consists of all the functions, 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 program How to create Checkbox in Kivy using a .kv file:

# main.py file
# program for creating a checkbox using .kv in kiws.

 
# import ki module

import kivy

 
# install the required version

kivy.require ( "1.9.0" )

 
# Your application base class inherits from the application class.
# app: always refers to your application instance

from kivy.app import App

 

from kivy.uix.boxlayout import BoxLayout

 
## not required I am using a .kv file

from kivy.uix.checkbox import CheckBox

 
# To do some window manipulation, import windows

from kivy.core. window import Window

  
# Container class for application widgets

class SampBoxLayout (BoxLayout):

 

# Callback for checkbox

def checkbox_click ( self , instance, value):

  if value is True :

print ( " Checkbox Checked " )

  else :

  print ( "Checkbox Unchecked" )

  

  
# Application derived from the App class

class SampleApp ( App):  

# build is the Kivy app class method used

# place widgets in the GUI.

def build ( self ):

# setting the background color of the window

Window.clearcolor = ( 0 , 0 , . 30 ,. 60 )

  return SampB oxLayout ()

 
# Rum app

root = SampleApp ()

root.run ()

sample.kv sample file.

# .kv file main.py

 
#: import CheckBox kivy.uix.checkbox

 
# color the label
& lt; CustLabel @ Label & gt ;:

color:. 761 ,. 190 ,. 810 , 1

 
& lt; SampBoxLayout & gt ;:

orientation: "vertical"

padding: 10

  spacing: 10

  

CustLabel:

text: "Gender"

  size_hint_x: 1

  font_size:   20

 

# create a box layout

BoxLayout:

# assigning orientation

  orientation: " horizontal "

  height: 20

  

BoxLayout:

  orientation: " horizontal "

  size_hint_x:. 22

  

  # create label

CustLabel:

text: "Male"

size_hint_x:. 80

  font_size: 30

CheckBox:

color :. 294 ,. 761 ,. 623

on_active: root.checkbox_click ( self , self . active)

size_hint_x:. 20

 

CustLabel:

text: " Female "

  size_hint_x:. 80

font_size: 20

CheckBox:

on_active: root.checkbox_click ( self , self . active)

  size_hint_x:. 20

 

CustLabel:

text: "Other"

size_hint_x:. 80

font_size: 10

CheckBox:

  on_active : root.checkbox_click ( self , self . active)

size_hint_x:. 20

Output:

Video output: