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