Create switch with Python-kivymd
Last Updated :
03 May, 2022
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
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 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.
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
class Test(MDApp):
def build( self ):
screen = Builder.load_string(KV)
return screen
Test().run()
|
Adding the above Steps:
Python3
from kivy.lang import Builder
from kivymd.app import MDApp
KV =
class Test(MDApp):
def build( self ):
screen = Builder.load_string(KV)
return screen
Test().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
from kivymd.app import MDApp
from kivymd.uix.screen import Screen
from 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
class MainApp(MDApp):
def build( self ):
screen = Screen()
wid = MDSwitch(pos_hint = { 'center_x' : 0.5 , 'center_y' : 0.5 })
screen.add_widget(wid)
return screen
MainApp().run()
|
Adding the above Steps:
Python3
from kivymd.app import MDApp
from kivymd.uix.screen import Screen
from kivymd.uix.selectioncontrol import MDSwitch
class MainApp(MDApp):
def build( self ):
screen = Screen()
wid = MDSwitch(pos_hint = { 'center_x' : 0.5 , 'center_y' : 0.5 })
screen.add_widget(wid)
return screen
MainApp().run()
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...