Change language

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

  
"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:

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically