Change language

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
"CustLabel @ Label & gt ;:

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

 
"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:

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method