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
Add comment