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

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically