In this video I’ll show you how to use CheckBoxes for Kivy and Python.

Check Boxes are super useful in just about any app. Kivy makes using CheckBoxes pretty easy, and I’ll show you how to do it in this video.

We’ll build a very basic pizza ordering app that lets us select toppings from a list of checkboxes.

We’ll then take some action based on whether or not we’ve checked a particular topping checkbox.

Python Code: check.py
GitHub Code: check.py

from kivy.app import App
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, topping):
		if value == True:
			MyLayout.checks.append(topping)
			tops = ''
			for x in MyLayout.checks:
				tops = f'{tops} {x}'
			self.ids.output_label.text = f'You Selected: {tops}'
		else:
			MyLayout.checks.remove(topping)
			tops = ''
			for x in MyLayout.checks:
				tops = f'{tops} {x}'
			self.ids.output_label.text = f'You Selected: {tops}'

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

if __name__ == '__main__':
	AwesomeApp().run()

Kivy Design Code: check.kv
GitHub Code: check.kv

<MyLayout>
	BoxLayout:
		orientation: "vertical"
		size: root.width, root.height
		
		Label:
			text: "Select Pizza Toppings"
			font_size:32

		GridLayout:
			cols:2

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

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

		Label:
			id: output_label
			text: ""
			font_size:32

John Elder

John is the CEO of Codemy.com where he teaches over 100,000 students how to code! He founded one of the Internet's earliest advertising networks and sold it to a publicly company at the height of the first dot com boom. After that he developed the award-winning Submission-Spider search engine submission software that's been used by over 3 million individuals, businesses, and governments in over 42 countries. He's written several Amazon #1 best selling books on coding, and runs a popular Youtube coding channel.

View all posts

Add comment

Your email address will not be published. Required fields are marked *

John Elder