Table of contents
1.
Introduction
2.
About kivy
3.
Switch Widget
3.1.
Implementation
3.2.
Output
4.
Attaching Switch to Callback
4.1.
Implementation
4.2.
Output1 
4.3.
Output2
5.
FAQs
5.1.
What is Kivy?
5.2.
What is the Switch widget in Kivy?
5.3.
What is the widget in Kivy?
5.4.
What is the root of Kivy?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Switch Widget in Kivy

Author Aniket Majhi
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Welcome readers! We hope you are doing well.

This blog is going to be interesting for you. Nowadays, Python has become one of the most important tools in developing different applications from a programmer's perspective. It has a lot of library support. If you want to impact the development field in Python, you need to learn more about different libraries in Python.

If you want to learn more about python, you can refer to the link Basics of Python.

In this blog, we will be discussing the Switch Widget in the Kivy library in Python. We will cover every aspect of the Switch Widget.

This blog will help enhance your knowledge about the Kivy library in Python and help you stand out from the crowd.

About kivy

Kivy is an open-source platform-independent GUI development python library for developing innovativemulti-touch UI applications. It can run on WindowsGNU/LinuxAndroidiOS etc.

Kivy is mainly used to develop Android Applications. It is also used in developing the Desktop Applications as well. 

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 PythonKivy 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!

Live masterclass