Python | Switch widget in Kivy

03 Nov, 2021
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.
 
The Switch widget is active or inactive, as a mechanical light switch. The user can swipe to the left/right to activate/deactivate it. The value represented by the switch is either True or False. That is the switch can be either in On position or Off position.To work with Switch you must have to import:  
from kivy.uix.switch import Switch
Note: If you want to control the state with a single touch instead of a swipe, use the ToggleButton instead. 
Basic Approach:

1) import kivy
2) import kivyApp
3) import Switch
4) import Gridlayout
5) import Label
6) Set minimum version(optional)
7) create Layout class(In this you create a switch)
8) create App class
9) return Layout/widget/Class(according to requirement)
10) Run an instance of the class
Implementation of the Approach:  
Python3
# Program to Show how to create a switch# import kivy module  import kivy   # base Class of your App inherits from the App class.  # app:always refers to the instance of your application from kivy.app import App # this restrict the kivy version i.e# below this kivy version you cannot# use the app or softwarekivy.require('1.9.0') # The Switch widget is active or inactive# The state transition of a switch is from# either on to off or off to on.from kivy.uix.switch import Switch # The GridLayout arranges children in a matrix.# It takes the available space and# divides it into columns and rows,# then adds widgets to the resulting “cells”.from kivy.uix.gridlayout import GridLayout # The Label widget is for rendering text.from kivy.uix.label import Label # A Gridlayout with a label a switch# A class which contains all stuff about the switchclass SimpleSwitch(GridLayout):      # Defining __init__ constructor     def __init__(self, **kwargs):           # super function can be used to gain access          # to inherited methods from a parent or sibling class          # that has been overwritten in a class object.          super(SimpleSwitch, self).__init__(**kwargs)           # no of columns          self.cols = 2           # Adding label to the Switch          self.add_widget(Label(text ="Switch"))           # Initially switch is Off i.e active = False          self.settings_sample = Switch(active = False)           # Add widget          self.add_widget(self.settings_sample)             # Defining the App Classclass SwitchApp(App):     # define build function     def build(self):          # return the switch class          return SimpleSwitch()   # Run the kivy appif __name__ == '__main__':     SwitchApp().run()
Output:  
 
Attaching Callback to Switch:  
A switch can be attached with a call back to retrieve the value of the switch.
The state transition of a switch is from either ON to OFF or OFF to ON.
When switch makes any transition the callback is triggered and new state can be retrieved i.e came and any other action can be taken based on the state.
By default, the representation of the widget is static. The minimum size required is 83*32 pixels.
The entire widget is active, not just the part with graphics. As long as you swipe over the widget’s bounding box, it will work.
Now For attaching a callback you have to define a callback function and bind it with the switch. So below is the code how to Attach a callback: 
Python3
# Program to Show how to attach a callback to switch # import kivy module  import kivy   # base Class of your App inherits from the App class.  # app:always refers to the instance of your application from kivy.app import App # this restrict the kivy version i.e# below this kivy version you cannot# use the app or softwarekivy.require('1.9.0') # The Switch widget is active or inactive# The state transition of a switch is from# either on to off or off to on.from kivy.uix.switch import Switch # The GridLayout arranges children in a matrix.# It takes the available space and# divides it into columns and rows,# then adds widgets to the resulting “cells”.from kivy.uix.gridlayout import GridLayout # The Label widget is for rendering text.from kivy.uix.label import Label # A Gridlayout with a label a switch# A class which contains all stuff about the switchclass SimpleSwitch(GridLayout):      # Defining __init__ constructor     def __init__(self, **kwargs):           # super function can be used to gain access          # to inherited methods from a parent or sibling class          # that has been overwritten in a class object.          super(SimpleSwitch, self).__init__(**kwargs)           # no of columns          self.cols = 2           # Adding label to the Switch          self.add_widget(Label(text ="Switch"))           # Initially switch is Off i.e active = False          self.settings_sample = Switch(active = False)           # Add widget          self.add_widget(self.settings_sample)           # Arranging a callback to the switch          # using bing function          self.settings_sample.bind(active = switch_callback)       # Callback for the switch state transition# Defining a Callback function# Contains Two parameter switchObject, switchValuedef switch_callback(switchObject, switchValue):         # Switch value are True and False    if(switchValue):        print('Switch is ON:):):)')    else:        print('Switch is OFF:(:(:(')   # Defining the App Classclass SwitchApp(App):     # define build function     def build(self):          # return the switch class          return SimpleSwitch()   # Run the kivy appif __name__ == '__main__':     SwitchApp().run()
Output:  
 
kashishsoda
sweetyty
Python-gui
Python-kivy
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Read JSON file using Python
Python map() function
Adding new column to existing DataFrame in Pandas
How to get column names in Pandas dataframe
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
Iterate over a list in Python