There are two kinds of checkboxes in Kivy, regular square checkboxes and those in a group. The checkboxes in a group look and act like circular radio buttons.
We use GridLayout as base for the root class. The StringProperty class is imported. In the example, we have 4 checkboxes. However, only for the first 3, we include Status strings. You can see the initial values of the Status strings.
These 2 functions will be called if either of the first 2 checkboxes is clicked. If so, the respective Status string will change.
These 2 functions will be called if either of the last 2 checkboxes is clicked. Note, in checkActive2, the state of checkbox is in the variable b. Also in checkActive3, we have logic that, should the user want the checkbox to become inactive, we tell Kivy to override this and set it back to active.
The app class is created and run. The title of the window is 'Checkboxes'.
# ex21.py from kivy.uix.gridlayout import GridLayout from kivy.app import App from kivy.properties import StringProperty class Ex21(GridLayout): Status=StringProperty("I am inactive") Status1=StringProperty("I am the inactive member of the group") Status2=StringProperty("I am the inactive member of the group") def checkActive(self,*args): if args[1]==True: self.Status="I am active" else: self.Status="I am inactive" def checkActive1(self,*args): if args[1]==True: self.Status1="I am the active member of the group" else: self.Status1="I am the inactive member of the group" def checkActive2(self,a,b): if b==True: self.Status2="I am active member of the group" else: self.Status2="I am the inactive member of the group" def checkActive3(self,*args): if args[1]==False: self.ids['pythonLang'].active=True class Ex21App(App): def build(self): self.title="Checkboxes" return Ex21() if __name__=='__main__': Ex21App().run()
In the kv file, we set columns as 3, and then set the background color as a gray color.
These are Kivy language commands for the first two rows. The second row contains a group checkbox.
These display the next 2 rows. The first one is part of a group. The last row has a checkbox which is always checked. Should someone try to inactivate it, the Python program will override that.
# ex21.kv <Ex21>: cols: 3 canvas: Color: rgb: .2,.2,.2 Rectangle: pos: self.pos size: self.size Label: text: 'A checkbox' CheckBox: on_active: root.checkActive(*args) Label: text: root.Status Label: text: 'A group checkbox' CheckBox: group: 'my_check_group' on_active: root.checkActive1(*args) Label: text: root.Status1 Label: text: 'A group checkbox' CheckBox: group: 'my_check_group' on_active: root.checkActive2(*args) Label: text: root.Status2 Label: text: 'Python is the best language' CheckBox: id: pythonLang active: True on_active: root.checkActive3(*args) Label: text: 'Right on'
This is the result with some clicked checkboxes.
Thanks for the post. I noticed that Kivy allows a situation where none of the radio buttons are selected. This breaks the established definition of what a group of buttons does. Hopefully they'll fix this at some point.
ReplyDeletePython:
ReplyDeleteGood Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
https://www.emexotechnologies.com/online-courses/python-training-in-electronic-city/
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
ReplyDeletePython Training in electronic city