How to Create Checkbox in Kivymd-Python
Last Updated :
02 Jun, 2022
In this article, we will see how to add the Check box in our application using KivyMD in Python. KivyMD is a collection of Material Design compliant widgets which can be used with Kivy.
Installation:
To install these modules type the below command in the terminal.
pip install kivy
pip install kivymd
We will be discussing how to create square and circular check box in this article.
Square Check Box
Step 1: Import required packages.
For this, we will need Builder from kivy and MDApp from kivymd package.
Note: We will not be importing MDFloatLayout because we are designing our screen using kv language.
Python3
# import packages
from kivy.lang import Builder
from kivymd.app import MDApp
Step 2: Design layout.
We will be designing our layout using kv language. First, we will declare the Layout widget class called MDFloatLayout and then the child widget class called MDCheckbox. We won't pass any parameters to MDFloatLayout and keep it default. For MDSwitch we will pass its location in x,y coordinate form. center_x is used for the x coordinate whereas center_y is used for the y coordinate. And we also pass check box size using the size method and pass the values in dp. We will also be using size_hint to reduce effective area while tapping on the check box and passing None in parameters.
Python3
# writing kv lang
KV = '''
# defining layout
MDFloatLayout:
# this will create check Box
MDCheckbox:
# defining size to check box
size: "48dp", "48dp"
size_hint: None,None
# giving location
pos_hint: {'center_x': .5, 'center_y': .5}
'''
Step 3: Writing the main program.
To run kv file we will be using load_string() and pass our kv language in it. So we will define a function for this named build() and on-call it will load kv and return the screen. run() is used to run the class and it does not require any parameters.
Python3
# app class
class Test(MDApp):
def build(self):
# this will load kv lang
screen = Builder.load_string(KV)
# returning screen
return screen
#running app
Test().run()
Adding the above steps:
Python3
# importing builder
from kivy.lang import Builder
# importing MDApp
from kivymd.app import MDApp
# writing kv lang
KV = '''
# defining layout
MDFloatLayout:
# this will create check Box
MDCheckbox:
# defining size to check box
size: "48dp", "48dp"
size_hint: None,None
# giving location
pos_hint: {'center_x': .5, 'center_y': .5}
'''
# app class
class Test(MDApp):
def build(self):
# this will load kv lang
screen = Builder.load_string(KV)
# returning screen
return screen
# running app
Test().run()
Output:
Circular check box
For this example, we need to make some changes in our kv file to get a circular check box and the other procedure and functions will remain same as used in the above example. To make the check box circular we need to pass the group in our kv file.
Syntax:
MDFloatLayout:
MDCheckBox:
group:'group'
Implementation:
Python3
# importing builder
from kivy.lang import Builder
# importing MDApp
from kivymd.app import MDApp
# writing kv lang
KV = '''
# defining layout
MDFloatLayout:
# this will make a circular check box
MDCheckbox:
group: 'group'
# defining size of check box
size_hint: None,None
size: "48dp", "48dp"
# giving location
pos_hint: {'center_x': .5, 'center_y': .5}
'''
# app class
class Test(MDApp):
def build(self):
# this will load kv lang
screen = Builder.load_string(KV)
# returning screen
return screen
# running app
Test().run()
Output:
Similar Reads
How to Create banner in kivymd-Python? In this article, we will see how to add the banner to our application using KivyMD in Python. A banner is a dropdown item when a button or action is triggered. Banners are widely used for pop-ups and warnings in mobile applications. You might need a basic understanding of kv lang to get started. Ins
5 min read
Python | Create checkbox using .kv file Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, Linux, and Windows etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications. Kivy Tutorial - Learn Kivy with Examples. Checkbox widget:Â Chec
3 min read
Python | Checkbox widget in Kivy Kivy is a platform independent GUI tool in Python. Kivy applications can be run on Android, IOS, linux, and Windows, etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktop applications. Kivy Tutorial - Learn Kivy with Examples. Checkbox
4 min read
How to Create Bottom Navigation using Kivymd and Python In this article, we will see how to add the Bottom Navigation in our application using KivyMD in Python. Installation: To install this module type the below command in the terminal. pip install kivy pip install kivymd MDBottomNavigation Method: Â Bottom navigation is used to navigate from one screen
5 min read
PyQt5 â Stop checking in CheckBox In this article we will see how to stop the check box to get checked. Sometimes, while creating forms there is a need to stop the check box to get checked. In order to this we have to use setCheckable method and set it to False, this will stop the button to get checked. Syntax : checkbox.setCheckabl
1 min read
Python | BoxLayout widget in Kivy Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, Linux and Windows, etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications. ???????? Kivy Tutorial - Learn Kivy with Examples. Now in this
5 min read
Create Toolbar in KivyMD In this article, we will see how to add the toolbar in mobile applications using KivyMD in Python. KivyMD provide two type of toolbar - Top ToolbarBottom Toolbar Let's see how to create each type of toolbar and how to add certain attributes like title, left menu, right menu, etc. Some commonly used
2 min read
Fonts in Kivymd-Python In this article, we will develop a GUI window using kivy framework of Python. Kivy is a platform-independent GUI tool in Python. It is an open-source Python library for the rapid development of multi-touch applications on Windows, macOS, Android, iOS, Linux, and Raspberry Pi. KivyMD package is a col
5 min read
How to make calculator using kivy | Python Kivy is a platform-independent GUI tool in Python. As it can be run on Android, IOS, linux and Windows etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications. ???????? Kivy Tutorial - Learn Kivy with Examples. In this artic
3 min read
How to run Python script directly in Kivy file? Kivy is a platform-independent GUI tool in Python. It can run on Android, IOS, Linux and Windows, etc. This is the only GUI library from python which can independently run on the android device even we can use it on Raspberry pi also. Â It is an open-source Python library for the rapid development of
3 min read