Python | Create checkbox using .kv file
Last Updated :
19 Oct, 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:
Python3
# main.py file
# program for creating checkbox using .kv in kivy.
# import kivy module
import kivy
# set require version
kivy.require("1.9.0")
# base Class of your App inherits from the App class.
# app:always refers to the instance of your application
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
## not necessary while using .kv file
from kivy.uix.checkbox import CheckBox
# To do some manipulation on window import 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")
else:
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()
# Run the app
root = SampleApp()
root.run()
sample.kv file of the code.
Python3
#.kv file of main.py file
#: import CheckBox kivy.uix.checkbox
# giving colour to label
<CustLabel@Label>:
color: .761, .190, .810, 1
<SampBoxLayout>:
orientation: "vertical"
padding: 10
spacing: 10
CustLabel:
text: "Gender"
size_hint_x: 1
font_size:20
# creating box layout
BoxLayout:
# assigning orientation
orientation: "horizontal"
height: 20
BoxLayout:
orientation: "horizontal"
size_hint_x: .22
# label creation
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:
Similar Reads
Python | Create Box Layout widget using .kv file Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, linux and Windows etc. Kivy provides you the functionality to write the code for once and run it on different platforms. It is basically used to develop the Android application, but it does not mean that it can not
3 min read
How to Create Checkbox in Kivymd-Python In this article, we will see how to add the Check box in our application using KivyMD in Python. KivyMD is a collection of Material Design compliant widgets which can be used with Kivy. Installation: To install these modules type the below command in the terminal. pip install kivy pip install kivym
3 min read
Python | Checkbox widget in Kivy Kivy is a platform independent GUI tool in Python. Kivy applications 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 Desktop applications. Kivy Tutorial - Learn Kivy with Examples. Checkbox
4 min read
Python - Change button color in kivy using .kv file 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. In this article we will learn how to change the background colo
3 min read
Python | Drop-down list in kivy using .kv file 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. Dro
3 min read