Switch Widget
The Switch widget is active or inactive as a mechanical switch. The user can swipe left or light to activate or deactivate the switch. It takes either a True or False value to represent the state of the switch.
Below, we will be showing the implementation of the switch widget.
Implementation
#Import the Kivy model
import kivy
from kivy.app import App
# If you want to work with the Switch, you must include this library
from kivy.uix.switch import Switch
# The GridLayout arranges children in a specified row and column size matrix.
# Each cell corresponds to each widget
from kivy.uix.gridlayout import GridLayout
# The Label widget is used to render the text.
from kivy.uix.label import Label
# The switchApp class, which contains all the stuff about the switch
class SwitchAppClass(GridLayout):
# Defining __init__ constructor
def __init__(self, **kwargs):
#super class constructor for gaining access to the inherited objects
super(SwitchAppClass, self).__init__(**kwargs)
# set the grid layout
self.cols = 1
self.rows = 1
# add the widget to only cell
# the active is set to false
# This means, by default, the switch is set to off
self.add_widget(Switch(active = False))
# Defining the App Class
class SwitchApp(App):
def build(self):
# return the SwitchApp class
return SwitchAppClass()
if __name__ == '__main__':
# run the kivy App
SwitchApp().run()

You can also try this code with Online Python Compiler
Run Code
Output

Attaching Switch to Callback
The switch callback function listens to the activation state. It is used to retrieve the value of the switch.
The callback function is triggered while the state of the switch is changed either from ON to OFF or OFF to ON.
Below is the implementation of the Switch with the callback function.
Implementation
import kivy
from kivy.app import App
from kivy.uix.switch import Switch
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
class SwitchAppClass(GridLayout):
def __init__(self, **kwargs):
super(SwitchAppClass, self).__init__(**kwargs)
self.cols = 1
self.rows = 1
self.switch = Switch(active = False)
self.add_widget(self.switch)
# attaching the callback function with the switch using the bind function
self.switch.bind(active = callback)
# definition of the callback function, it just takes the value of the switch
# and prints the status of the switch
def callback(instance , value):
print("ON" if value else "OFF")
class SwitchApp(App):
def build(self):
return SwitchAppClass()
if __name__ == '__main__':
SwitchApp().run()

You can also try this code with Online Python Compiler
Run Code
Output1

Output2

FAQs
What is Kivy?
Kivy is an open-source platform-independent GUI development python library for developing applications.
What is the Switch widget in Kivy?
The Switch widget is active or inactive as a mechanical switch.
What is the widget in Kivy?
The widget in Kivy is the base building block of the GUI interface.
What is the root of Kivy?
The root inside a kv file refers to the parent with the angular brackets.
Conclusion
In this article, we have extensively discussed the Switch Widget in Kivy.
- We started with the basic introduction.
- We discussed the Kivy.
- We looked at what a Switch widget is in Kivy.
-
We showed the implementation of the Switch widget in Kivy with and without attaching the callback function.
We hope that this blog has helped you enhance your knowledge regarding Switch Widget in Kivy. If you would like to learn more, check out our articles on Open Source Python Libraries and Application Development in Python, Kivy Float Layout in Python, and Popular Python Libraries. Do upvote our blog to help other ninjas grow.
Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, follow our guided paths, and crack product based companies Interview Bundle.
Happy Reading!