Table of contents
1.
Introduction
2.
CheckBox Widget
3.
Implementation
3.1.
Code 1 (Python)
3.2.
Code 2 (Kivy)
3.3.
Steps to Run the Code
3.4.
Output
4.
Frequently Asked Questions
4.1.
What is the purpose of Kivy?
4.2.
Why isn't kivy popular?
4.3.
Is kivy quick?
4.4.
Which is the most effective, Django or Kivy?
4.5.
What exactly is a Kivy designer?
5.
Conclusion 
Last Updated: Mar 27, 2024
Easy

Checkbox Widget in Kivy

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Checkboxes are one of the most common User Interface (UI) controls that we can use to allow users to make choices. We have seen these checkboxes everywhere in our daily life, from performing checkable interactive controls such as Terms and Conditions Checkboxes to verifying a user “ not as a Robot” using CAPTCHA.

Checkboxes are typically displayed on screens as square boxes with white spaces (for false, i.e., not checked) or a tick mark or X. (for true, i.e., checked).

                                               

                                                                      Source    

Checkboxes are handy in almost any app. Kivy makes it simple to use CheckBoxes.This article will explain how to use Checkbox Widget in Kivy.

CheckBox Widget

CheckBoxes are two-state buttons that can be checked or unchecked. Checkboxes are accompanied by a label that explains what the checkbox is for.

We will create a simple checkbox application that allows us to select programming languages from a list of checkboxes. 

To work with Checkbox Widget, we will import Checkbox from kivy.uix.checkbox Module.

Syntax:

from kivy.uix.checkbox import CheckBox
You can also try this code with Online Python Compiler
Run Code

Implementation

Here's a basic demonstration of using checkboxes in a Kivy application. After writing the code in an editor, save the file using the .py extension.

Filename: check.py

Code 1 (Python)

from kivy.app import App
# Required for creating widgets
from kivy.uix.widget import Widget
from kivy.lang import Builder

# Designate Our .kv design file 
Builder.load_file('check.kv')

class MyLayout(Widget):
checks = []
	def checkbox_click(self, instance, value, languages):
		if value == True:
			MyLayout.checks.append(languages)
			lang = ''
			for x in MyLayout.checks:
			tops = f'{lang} {x}'
			self.ids.output_label.text = f'You Selected: {lang}'
		else:
			MyLayout.checks.remove(languages)
			lang = ''
			for x in MyLayout.checks:
			tops = f'{lang} {x}'
			self.ids.output_label.text = f'You Selected: {lang}'

class Code_StudioApp(App):
def build(self):
return MyLayout()

if __name__ == '__main__':
Code_StudioApp().run()
You can also try this code with Online Python Compiler
Run Code

Writing everything in the same code leads to a jumbled mess that is difficult to understand by others. Hence we are using another file check.kv, which enables us to build our widget tree in a declarative manner and helps to call back widget properties properly.

Filename: check.kv

Code 2 (Kivy)

<MyLayout>
BoxLayout:
orientation: "vertical"
size: root.width, root.height

	Label:
	text: "Select Your Favourite Programming Languages!!"
	font_size:32

GridLayout:
cols:2

	Label:
	text: "Python"
	font_size:20
	
CheckBox:
on_active: root.checkbox_click(self, self.active, "Python")

	Label:
	text: "Java"
	font_size:20
	
CheckBox:
on_active: root.checkbox_click(self, self.active, "Java")

	Label:
	text: "CSharp"
	font_size:20
		
CheckBox:
      on_active: root.checkbox_click(self, self.active, "CSharp") 

	Label:
	id: output_label
	text: ""
	font_size:32
You can also try this code with Online Python Compiler
Run Code

We have used the Builder Method in our check.py file to bind both files.

Syntax:

from kivy.lang import Builder
Builder.load_file('file_name')
You can also try this code with Online Python Compiler
Run Code

Steps to Run the Code

To run our program, consider the following steps:

Step 1: Open the Anaconda Prompt and enter the correct path to our kivy programme.

 

Step 2: Use the following command and press Enter to run the programme.

Python file_name.py (Example: check.py)

Output

Here is the output of our simple checkbox application that allows us to select programming languages from a list of checkboxes. Code_Studio is the name of our application.

Frequently Asked Questions

What is the purpose of Kivy?

Kivy is a fascinating GUI framework that can be used to construct desktop user interfaces and mobile apps for iOS and Android. Kivy applications on any platform will not look like native apps.

 

Why isn't kivy popular?

It's because all Android programmers prefer to work using Java. The majority of mobile software is created with an android studio or kotlin, both of which are java-based.

 

Is kivy quick?

Kivy is extremely quick and efficient for various tasks; for example, you can effortlessly push and manipulate millions of vertices with complicated effects with no noticeable lag.

 

Which is the most effective, Django or Kivy?

Both are beneficial to learn. Django is for creating web programs (and web APIs). Kivy is a Python framework for creating mobile apps. Both are commonly used for different purposes, but you might combine them (A kivy app with Django as a backend).

 

What exactly is a Kivy designer?

Kivy Designer is a tool used by Kivy to create graphical user interfaces (GUIs) using Kivy Widgets. Widgets can be made, customised, and tested. Kivy is used to writing it entirely in Python.

Conclusion 

This article extensively discussed the Checkbox Widget in Kivy, its Implementation, and the steps to run the code using the Anaconda command prompt. Check out the article on the 19 best Python frameworks to know more about different Python frameworks.

We hope this blog has helped you enhance your knowledge regarding Kivy. You can learn more about Python by visiting the Python category in Coding Ninjas Blogs. Visit our awesome articles to understand basic concepts of Python like Arrays, Lists, Iterators, generatorsVariablesFunctions, etc.

Upvote our blog to help other ninjas grow.

Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, interview bundle, follow guided paths for placement preparations, and much more!!

We wish you Good Luck! Keep coding and keep reading Ninja!!

 

Live masterclass