Python | Spinner widget in kivy

06 Feb, 2020
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.
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 configfrom kivy.config import Config  # 0 being off 1 being on as in true / false# you can use 0 or 1 && True or FalseConfig.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 listfrom 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 classclass 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 appif __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 configfrom kivy.config import Config  # 0 being off 1 being on as in true / false# you can use 0 or 1 && True or FalseConfig.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 listfrom 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 classclass 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 appif __name__ == '__main__':    SpinnerExample().run()      
Output:
Image 1:
Image 2:
Below is the output in video to get better understanding:
Media error: Format(s) not supported or source(s) not found
Python-gui
Python-kivy
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
Iterate over a list in Python