Python | Spinner 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.

Spinner widget:

To work with a spinner, you need to import:

 from kivy.uix.spinner import Spinner 

Spinner — it is a widget that provides a quick way to select one value from a set. In the default state, the counter shows its currently selected value. Touching the counter displays a drop-down menu with all other available values ​​from which the user can select a new one. 
As with the combo box, the counter object can have multiple values, and you can select one of the values. 
A callback can be attached to the spinner object to receive notifications about the selection of a value from the spinner object.

  Basic Approach:  1) import kivy 2) import kivyApp 3) import spinner 4) import Floatlayout (according to need) 5) import window (optional) 6) Set minimum version (optional) 7) Create Layout class: define the clicked function in it 8) Create App class 9) create .kv file (name same as the app class): 1) create Spinner 2) create callback 3) And many more styling as needed 10) return Layout / widget / Class (according to requirement) 11) Run an instance of the class 

Below is the implementation:

In the code below, we created a spinner, performed resizing and positioning, and attached a callback to the values.

.py file:

# Sample Spinn application er in KIVY using a .kv file

 
# to change kivy settings by by default we use this module config

from kivy.config import Config

  
# 0 disabled 1 enabled as true / false
# You can use 0 or 1 & amp; & amp; True or False

Config. set ( `graphics` , ` resizable` , True )

 
# Program to show how to create a radio button
# import nodded module

import kivy 

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

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` )

  
# Spinner is a widget that provides
# a quick way to select one value from a set.
# as a drop-down list

from kivy. uix.spinner import Spinner

 
# the module consists of floatlayout
# work with FloatLayout first
# you must import it

from kivy.uix.floatlayout import FloatLayout

 
# Here to provide background color

from kivy.core.window import Window

 

 
# create LayoutClass

class SampBoxLayout (FloatLayout):

# For a Spinner function that defines a spinner

def spinner_clicked ( self , value):

print ( "Language selected is" + value)

  

  
# # Derive from the App class

class SampleApp (App):

def build ( self ):

 

# Set the background color for the window

Window.clearcolor = ( 0.555 , 0.261 ,. 888 , 0.5 )

return SampBoxLayout ()

 
# create object for AppClass

root = SampleApp ()

# run the class
root.run ()

 

.kv file code:

# .k v implementation of the .py file

 
# Create a layout, i.e. the root of the layout class
& lt; SampBoxLayout & gt ;:

 

# creating spinners

Spinner:

# Assigning an identifier

id : spinner_id

 

# Call back

on_text: root.spinner_clicked (spinner_id.text)

 

# initially text on counter

 text: "Python"

 

# total counter

values: [ "Python" , " Java " , " C ++ " , "C" , "C #" , "PHP" ]

  

# spinner size declaration

# and its position

  size_hint: None , None

  size: 200 , 50

  pos_hint: { `center_x` :. 5 , `top` : 1 }

Exit:

Image 1:

Image 2:

Image 3:

Below here is the output in the video to understand better: