Saturday, November 16, 2024
Google search engine
HomeLanguagesPython | Checkbox widget in Kivy

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 widget –
CheckBox is a specific two-state button that can be either checked or unchecked. Checkboxes have an accompanying label that describes the purpose of the checkbox. Checkboxes can be grouped together to form radio buttons. Checkboxes are used to convey whether a setting is to be applied or not.

To work with the Checkbox, you first have to import Checkbox from the module which consists all features, functions of the slider i.e.  

 from kivy.uix.checkbox import CheckBox 

Basic Approach to follow while creating Slider : 

1) import kivy
2) import kivy App
3) import gridlayout
4) import Label
5) import Checkbox
6) import Widget
7) set minimum version(optional)
8) Extend the class
9) Add widget in the class
10) Create the App class
11) run the instance of the class

Now the program of How to create Checkbox in Kivy:  

Python3




# Program to learn how to make checkbox in kivy
 
# import kivy module
import kivy
 
# base Class of your App inherits from the App class. 
# app:always refers to the instance of your application 
from kivy.app import App
 
# The :class:`Widget` class is the base class
# required for creating Widgets.
from kivy.uix.widget import Widget
 
# The Label widget is for rendering text.
from kivy.uix.label import Label
 
# To use the checkbox must import it from this module
from kivy.uix.checkbox import CheckBox
 
# The GridLayout arranges children in a matrix.
from kivy.uix.gridlayout import GridLayout
 
  
# Container class for the app's widgets
class check_box(GridLayout):
 
    def __init__(self, **kwargs):
        # super function can be used to gain access
        # to inherited methods from a parent or sibling class
        # that has been overwritten in a class object.
        super(check_box, self).__init__(**kwargs)
 
        # 2 columns in grid layout
        self.cols = 2
 
        # Add checkbox, widget and labels
        self.add_widget(Label(text ='Male'))
        self.active = CheckBox(active = True)
        self.add_widget(self.active)
 
        self.add_widget(Label(text ='Female'))
        self.active = CheckBox(active = True)
        self.add_widget(self.active)
 
        self.add_widget(Label(text ='Other'))
        self.active = CheckBox(active = True)
        self.add_widget(self.active)
   
       
# App derived from App class
class CheckBoxApp(App):
    def build(self):     
        return check_box()
 
# Run the app
if __name__ == '__main__':
    CheckBoxApp().run()


Output: 

Now Question is that How can we bind or attach callback to Checkbox? 
So the simple example is given which bind Checkbox with the click i.e when it clicked it print “Checkbox Checked” else it will print “Checkbox unchecked”.

Now program to arrange a callback to Checkbox i.e whether checkbox is checked or not.  

Python3




# Program to learn how to make checkbox
# and adding callback in kivy
 
# import kivy module
import kivy
 
# base Class of your App inherits from the App class. 
# app:always refers to the instance of your application 
from kivy.app import App
 
# The :class:`Widget` class is the base class
# required for creating Widgets.
from kivy.uix.widget import Widget
 
# The Label widget is for rendering text.
from kivy.uix.label import Label
 
# To use the checkbox must import it from this module
from kivy.uix.checkbox import CheckBox
 
# The GridLayout arranges children in a matrix.
# imports the GridLayout class for use in the app.
from kivy.uix.gridlayout import GridLayout
 
  
# Container class for the app's widgets
class check_box(GridLayout):
 
    def __init__(self, **kwargs):
        # super function can be used to gain access
        # to inherited methods from a parent or sibling class
        # that has been overwritten in a class object.
        super(check_box, self).__init__(**kwargs)
 
        # 2 columns in grid layout
        self.cols = 2
 
        # Add checkbox, Label and Widget
        self.add_widget(Label(text ='Male'))
        self.active = CheckBox(active = True)
        self.add_widget(self.active)
 
        # Adding label to screen
        self.lbl_active = Label(text ='Checkbox is on')
        self.add_widget(self.lbl_active)
         
 
        # Attach a callback
        self.active.bind(active = self.on_checkbox_Active)
 
  
    # Callback for the checkbox
    def on_checkbox_Active(self, checkboxInstance, isActive):
        if isActive:
            self.lbl_active.text ="Checkbox is ON"
            print("Checkbox Checked")
        else:
            self.lbl_active.text ="Checkbox is OFF"
            print("Checkbox unchecked")
  
 
# App derived from App class
class CheckBoxApp(App):
    def build(self):
        # build is a method of Kivy's App class used
        # to place widgets onto the GUI.
        return check_box()
 
# Run the app
if __name__ == '__main__':
    CheckBoxApp().run()


Output: 

Video Output:  

  
Reference: https://kivy.org/doc/stable/api-kivy.uix.checkbox.html.
 

RELATED ARTICLES

Most Popular

Recent Comments