Create switch with Python-kivymd

In this article, we will see how to add the switch in our application using KivyMD in Python.
MDSwitch: The switch is a kind of toggle button mostly used in android apps to on/off features.Its looks as:
Installation:
To install the modules type the below command in the terminal.
pip install kivy
pip install kivymd
Method 1: Using kv language:
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 and MDSwitch because we are designing our screen using kv language.
Python3
# import packagesfrom kivy.lang import Builderfrom 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 MDSwitch. We won’t pass any parameters to MDFloatLayout and keep it default.
For MDSwitch we will pass it’s location in x,y coordinate form. center_x is used for the x coordinate whereas center_y is used for the y coordinate.
Python3
# writing kv langKV = '''# declaring layoutMDFloatLayout: # this will create a switch MDSwitch: # giving position to the switch on screen 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 classclass Test(MDApp): def build(self): # this will load kv lang screen = Builder.load_string(KV) # returning screen return screen# running appTest().run() |
Adding the above Steps:
Python3
# importing packagesfrom kivy.lang import Builderfrom kivymd.app import MDApp# writing kv langKV = '''# declaring layoutMDFloatLayout: # this will create a switch MDSwitch: # giving position to the switch on screen pos_hint: {'center_x': .5, 'center_y': .5}'''# app classclass Test(MDApp): def build(self): # this will load kv lang screen = Builder.load_string(KV) # returning screen return screen# running appTest().run() |
Output:
Method 2. Without kv language:
Step 1. Import required packages.
For this, we will need Screen, MDSwitch, and MDApp from kivymd package.
Python3
# import packagesfrom kivymd.app import MDAppfrom kivymd.uix.screen import Screenfrom kivymd.uix.selectioncontrol import MDSwitch |
Step 2. Writing the main program.
To design a layout first we will need a black layout, for which we will use screen(). Now we need to define MDSwitch. We can do that using MDSwitch(), In this method, we used the same parameter used in method 1 which will be used to define their location on-screen using x,y coordinates. After all this, we have to MDSwitch widget to the screen and to do so we will be using add.widget(), where parameters will be the widget that we want to add to the screen. And that’s all now we will run the app class using run().
Python3
# App classclass MainApp(MDApp): def build(self): # defining blank screen/layout. screen = Screen() # defining MDSwitch widget and storing in a variable wid = MDSwitch(pos_hint={'center_x': 0.5, 'center_y': 0.5}) # adding widget to the screen screen.add_widget(wid) # returns screen/layout return screen# running appMainApp().run() |
Adding the above Steps:
Python3
# import packagesfrom kivymd.app import MDAppfrom kivymd.uix.screen import Screenfrom kivymd.uix.selectioncontrol import MDSwitch# App classclass MainApp(MDApp): def build(self): # defining blank screen/layout. screen = Screen() # defining MDSwitch widget and storing in a variable wid = MDSwitch(pos_hint={'center_x': 0.5, 'center_y': 0.5}) # adding widget to the screen screen.add_widget(wid) # returns screen/layout return screen# running appMainApp().run() |
Output:



