Open In App

Python | Spinner widget in kivy

Last Updated : 06 Feb, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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.

👉🏽 Kivy Tutorial – Learn Kivy with Examples.

Spinner widget :

To work with spinner you must have to import:

from kivy.uix.spinner import Spinner

Spinner is a widget that provides a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all the other available values from which the user can select a new one.
Like a combo box, a spinner object can have multiple values and one of the values can be selected.
A callback can be attached to the spinner object to receive notifications on selection of a value from the spinner object.

Basic Approach :

1) import kivy
2) import kivyApp
3) import Label
4) import Spinner
5) import Floatlayout
6) Set minimum version(optional)
7) create App class:
        1) Create the spinner
        2) Attach the labels to spinners
        3) Attach a callback also 
8) return Layout/widget/Class(according to requirement)
9) Run an instance of the class

Implementation of a simple spinner:




# Sample spinner app in kivy to change the
# kivy default settings we use this module config
from kivy.config import Config
  
# 0 being off 1 being on as in true / false
# you can use 0 or 1 && True or False
Config.set('graphics', 'resizable', True)
  
# Program to Show how to create a switch
# import kivy module   
import kivy 
     
# base Class of your App inherits from the App class.   
# app:always refers to the instance of your application  
from kivy.app import App
   
# this restrict the kivy version i.e 
# below this kivy version you cannot 
# use the app or software 
kivy.require('1.9.0')
  
# The Label widget is for rendering text. 
from kivy.uix.label import Label
  
# Spinner is a widget that provides a
# quick way to select one value from a set.
# like a dropdown list
from kivy.uix.spinner import Spinner
  
# module consist the floatlayout 
# to work with FloatLayout first 
# you have to import it 
from kivy.uix.floatlayout import FloatLayout
  
  
# Make an App by deriving from the App class
class SpinnerExample(App):
  
    # define build 
    def build(self):
  
        # creating floatlayout
        layout = FloatLayout()
  
        # creating the spinner
        # configure spinner object and add to layout
        self.spinnerObject = Spinner(text ="Python",
             values =("Python", "Java", "C++", "C", "C#", "PHP"),
             background_color =(0.784, 0.443, 0.216, 1)) 
  
        self.spinnerObject.size_hint = (0.3, 0.2)
  
        self.spinnerObject.pos_hint ={'x': .35, 'y':.75}
  
        layout.add_widget(self.spinnerObject)
  
        # return the layout
        return layout;
  
  
# Run the app
if __name__ == '__main__':
    SpinnerExample().run()      


Output:

Image 1:

Image 2:

Now if we have to tell user every time which element in a list is selected, we will display a label just beside the spinner which tells about the selected label. Also, we will print the value, and text of spinner.

Below is the Implementation:




# Sample spinner app in kivy to change the
# kivy default settings we use this module config
from kivy.config import Config
  
# 0 being off 1 being on as in true / false
# you can use 0 or 1 && True or False
Config.set('graphics', 'resizable', True)
  
# Program to Show how to create a switch
# import kivy module   
import kivy 
     
# base Class of your App inherits from the App class.   
# app:always refers to the instance of your application  
from kivy.app import App
   
# this restrict the kivy version i.e 
# below this kivy version you cannot 
# use the app or software 
kivy.require('1.9.0')
  
# The Label widget is for rendering text. 
from kivy.uix.label import Label
  
# Spinner is a widget that provides a
# quick way to select one value from a set.
# like a dropdown list
from kivy.uix.spinner import Spinner
  
# module consist the floatlayout 
# to work with FloatLayout first 
# you have to import it 
from kivy.uix.floatlayout import FloatLayout
  
  
# Make an App by deriving from the App class
class SpinnerExample(App):
  
    # define build 
    def build(self):
  
        # creating floatlayout
        layout = FloatLayout()
  
        # creating the spinner
        # configure spinner object and add to layout
        self.spinnerObject = Spinner(text ="Python",
              values =("Python", "Java", "C++", "C", "C#", "PHP"),
              background_color =(0.784, 0.443, 0.216, 1)) 
  
        self.spinnerObject.size_hint = (0.3, 0.2)
  
        self.spinnerObject.pos_hint ={'x': .35, 'y':.75}
  
        layout.add_widget(self.spinnerObject)
        self.spinnerObject.bind(text = self.on_spinner_select)
  
        # It changes the label info as well
        # add a label displaying the selection from the spinner
        self.spinnerSelection = Label(text ="Selected value in spinner is: %s" 
                                                     %self.spinnerObject.text)
  
        layout.add_widget(self.spinnerSelection)
        self.spinnerSelection.pos_hint ={'x': .1, 'y':.3}
          
        return layout;
  
    # call back for the selection in spinner object
    def on_spinner_select(self, spinner, text):
        self.spinnerSelection.text = "Selected value in spinner is: %s"
                                              %self.spinnerObject.text)
  
        print('The spinner', spinner, 'have text', text)
      
  
# Run the app
if __name__ == '__main__':
    SpinnerExample().run()      


Output:

Image 1:

Image 2:

Below is the output in video to get better understanding:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads