Python | Toggle button in kiw 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 Button:

The ToggleButton widget acts like a checkbox. When you touch or click on it, the state toggles between “normal” and “down” (as opposed to a button that is only “down” while pressed).

Toggle buttons can also be grouped to create switches — only one button in a group can be in the down state. The group name can be a string or any other Python hashable object:

 btn1 = ToggleButton (text = `Male`, group =` sex`,) btn2 = ToggleButton (text = `Female`, group =` sex `, state =` down`) btn3 = ToggleButton (text = `Mixed`, group =` sex`) 

Only one of the buttons can be pressed / disabled at the same time. You can use the same properties to customize the ToggleButton as for the Button class.

  Basic Approach:  1) import kivy 2) import kivyApp 3) import toggle button 4) import Gridlayout 5) Set minimum version (optional) 6) create layout class 7) create App class 8) create the, kv file 9) return Layout / widget / Class (according to requirement) 10) Run an instance of the class 

Implementation of the approach:

.py code:

# A program to explain how to use the Toggle button in nodding

 
# import nodded module

import kivy 

 
# The base class of your application inherits from the class of the application 
# 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` )

  
# The ToggleButton widget acts like a checkbox.
# To use this, you must import it.

from kivy.uix.togglebutton import T oggleButton

 
# GridLayout arranges children in a matrix.
# It takes up available space and divides it
# into columns and rows, then adds
# widgets for the received "cells".

from kivy. uix.gridlayout import GridLayout

 

 
# Create layout class

class Toggle_btn (GridLayout):

pass

 
# Create application class

class ToggleApp (App):

def build ( self ):

return Toggle_btn ()

 
# Run the application

if __ name__ = = `__main__` :

  ToggleApp () .run ()

Code .kv :

# .kv code file implementation

  
& lt; Toggle_btn & gt ;:

 

# Colums divides the screen in two

cols: 2

 

# Create Toogle 1 button

RelativeLayout:

  canvas:

Color:

rgb: 0 , 0 , 1

  Rectangle:

  size: root.width, root.height

ToggleButton:

size_hint: None , None

  size: 0.25 * root.width, 0.25 * root.height

pos: 0.125 * root.width, 0.350 * root.height

< code class = "undefined spaces">  text: `Toggle Button 1`

group: `geometry`

 

# Create a Toogle 2 button

RelativeLayout:

canvas:

Color:

rgb: 0 , 1 , 1

Rectangle:

size: root.width, root.height

  ToggleButton:

size_hint: None , None

size: 0.25 * root.width, 0.25 * root.height

pos: 0.125 * root.width, 0.350 * root .height

text: `Toggle Button 2`

group: `geometry`

Output: