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.
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
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:
- Python | Switch widget in Kivy using .kv file
- Python | Add image widget in Kivy
- Python | Checkbox widget in Kivy
- Python | Slider widget in Kivy
- Python | Carousel Widget In Kivy
- Python | BoxLayout widget in Kivy
- Python | Popup widget in Kivy
- Python | Spinner widget in kivy
- Python | Scrollview widget in kivy
- Python | Textinput widget in kivy
- Python | Progress Bar widget in kivy
- Python | Carousel Widget In Kivy using .kv file
- Python | Popup widget in Kivy using .kv file
- Python | Spinner widget in Kivy using .kv file
- Python | Progressbar widget in kivy using .kv file
- Python Tkinter - ScrolledText Widget
- Progressbar widget in Tkinter | Python
- Python Tkinter - Entry Widget
- Python Tkinter - Checkbutton Widget
- Python | Slider widget using .kv file
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.