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

Radio Buttons allow the user to make a selection from a group of things. They only allow one choice to be selected, as opposed to check boxes that allow for multiple selections.

Using Radio Buttons with Kivy is super easy, they’re exactly the same as Check Boxes, you just add a group name. That’s it! I’ll show you how in this video.

Python Code: radio.py
GitHub Code: radio.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('radio.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: radio.kv
GitHub Code: radio.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:
				group: "pizza_toppings"
				on_active: root.checkbox_click(self, self.active, "Pepperoni")

			Label:
				text: "Cheese"
				font_size:20
			CheckBox:
				group: "pizza_toppings"
				on_active: root.checkbox_click(self, self.active, "Cheese")
				
			Label:
				text: "Mushroom"
				font_size:20
			CheckBox:
				group: "pizza_toppings"
				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