Python | Switch widget in Kivy using .kv file



Kivy — it is a platform independent GUI tool in Python. Since it can run on Android, IOS, Linux, Windows, etc. It is mainly used to develop Android application, but that does not mean that it cannot be used in desktop applications.

Toggle widget:

Widget A switch is active or inactive, like a mechanical light switch. User can swipe left / right to activate / deactivate it. 
The value represented by the radio button is True or False. That is, the switch can be either on or off.

To work with Switch, you need to import:

 from kivy.uix.switch import Switch 

Attaching a callback to the switch:

  • A switch can be attached with a callback to get the switch value.
  • Switch state transition from ON to OFF or OFF to ON.
  • When the switch makes any transition, a callback is called and the new state can be restored, i.e. has arrived and any other action can be performed based on the state.
  • By default, the widget view is static. The minimum required size is 83 * 32 pixels.
  • The entire widget is active, not just the graphics part. As long as you swipe over the widget`s bounding box, it will work.
  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): - & gt; define the callback of the switch in this 8) create App class 9) create .kv file (name same as the app class): 1) create boxLayout 2) Give Lable 3) Create Switch 4) Bind a callback if needed 10) return Layout / widget / Class (according to requirement) 11) Run an instance of the class 

Below is the implementation:

We have explained how to create a button, attach a callback to it and how to disable the button after it is activated / inactive.

main.py file:

# A program to explain how the switch works

  
# import nodding module

import kivy 

  
# Your application base class inherits from the application class.
# app: always refers to your application instance

from kivy.app import App

 
# this limits the kivy version ie
# below this version you cannot
# use the application or software

kivy .require ( `1.9.0` )

  
# Switch widget is active or inactive
# Switch state transition from
# either enabled or disabled to enable.

from kivy.uix.sw itch import Switch

  
# GridLayout arranges children in a matrix.
# It takes up 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 designed to render text .

from kivy.uix.label import Label

 
# Gridlayout labeled with reversal switch
# The class that contains everything about the switch

class SimpleSwitch (GridLayout):

 

number of rows

  rows = 4

 

# Callback for switch state transition

  # Callback function definition

  # Contains two switchObject parameters, switchValue

  def switch_callback ( self , switchObject, switchValue):

 

# Toggle True and False

if (switchValue):

print ( `Switch is ON: ) :) :) ` )

  else :

print ( `Switch is OFF :( :( :(` )

 

 
# Class definition and applications

class SwitchApp (App):

# define build function

def build ( self ):

# return switch class

return SimpleSwitch ()

 

  
# Run the KIVY app

if __ name__ = = `__main__` :

  SwitchApp (). run ()

# .kv file with entire switch functions
# Along with labels there are

 
& lt; SimpleSwitch & gt ;:

 

# create a box layout for a better view

BoxLayout:

size_hint_y: None

  height: ` 48dp`

  

# Add toggle tag

Label:

text: `Switch normal`

  

# create a radio button

  Switch:

 

  # False means OFF and True means ON

active: False

  

  # Call back to the switch

on_active: root.switch_callback ( self , self . active)

  

# Another for another switch

 

BoxLayout:

size_hint_y: None

height: `48dp`

  

Label:

< code class = "undefined spaces">  text: `Switch active`

Switch:

active: True

on_active: root.switch_callback ( self , self . active)

 

  

BoxLayout:

size_hint_y: None

height: ` 48dp`

  

Label:

text: `Switch off & amp; disabled`

 

Switch:

# disabled True means after switching False

# it is disabled, now you cannot change its state

disabled: True

active: False

  

  BoxLayout:

size_hint_y: None

height: `48dp`

 

Label :

text: `Switch on & amp; disabled`

Switch:

disabled: True

active: True

Exit:

Image 1:

Image 2:

Image for displaying callbacks: