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
# 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 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 lang
KV = '''
# declaring layout
MDFloatLayout:
# 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 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 packages
from kivy.lang import Builder
from kivymd.app import MDApp
# writing kv lang
KV = '''
# declaring layout
MDFloatLayout:
# this will create a switch
MDSwitch:
# giving position to the switch on screen
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:
Method 2. Without kv language:
Step 1. Import required packages.
For this, we will need Screen, MDSwitch, and MDApp from kivymd package.
Python3
# import packages
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
# App class
class 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 app
MainApp().run()
Adding the above Steps:
Python3
# import packages
from kivymd.app import MDApp
from kivymd.uix.screen import Screen
from kivymd.uix.selectioncontrol import MDSwitch
# App class
class 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 app
MainApp().run()
Output:
Similar Reads
Python | Switch 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. Switch widget
5 min read
Clock App with Kivy using Python In this article, we are going to see how to develop a Clock Application with Kivy using Python. Kivy is a graphical user interface opensource Python library that allows you to develop multi-platform applications on Windows, macOS, Android, iOS, Linux, and Raspberry-Pi. In addition to the regular mou
4 min read
Python | Switch widget in Kivy 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. Switch widge
4 min read
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
Create spinner using KivyMD In this article, we will learn how to create a spinner using KivyMD in Python programming language. Create spinner using KivyMD In addition to the Kivy framework is KivyMD. A set of Material Design widgets for use with the Kivy GUI framework which is used to create mobile applications. The Kivy fram
4 min read
Python | Working with buttons in Kivy Kivy is a platform-independent GUI tool in Python as it can be run on Android, IOS, Linux Windows, etc. Kivy provides you the functionality to write the code for once and run it on different platforms. It is basically used to develop the Android application, but it does not mean that it can not be u
5 min read
Python | Slider 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. Slider: To w
4 min read
How to Create Checkbox in Kivymd-Python 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 kivym
3 min read
Python | Create Box Layout widget using .kv file Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, linux and Windows etc. Kivy provides you the functionality to write the code for once and run it on different platforms. It is basically used to develop the Android application, but it does not mean that it can not
3 min read
Python | Spinner 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. Spi
5 min read