Spinner Widget In Kivy With Example

Overview:

  • Kivy offers a spinner object for selecting a value from multiple values.

 

  • Like a combo box a spinner object can have multiple values and one of  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.

Example:

# Sample spinner app in kivy 
from kivy.uix.label import Label
from kivy.uix.spinner import Spinner

from kivy.uix.floatlayout import FloatLayout

from kivy.app import App

# Make an App by deriving from the App class
class SpinnerExample(App):
    def build(self):
        layout = FloatLayout()
        
        # configure spinner object and add to layout
        self.spinnerObject = Spinner(text="Python", values=("Python", "Java", "C++")) 
        self.spinnerObject.size_hint  = (0.3, 0.2)
        self.spinnerObject.pos_hint={'x': .1, 'y':.75}
        layout.add_widget(self.spinnerObject)
        self.spinnerObject.bind(text=self.on_spinner_select)
        
        # 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:

Spinner widget in Kivy - Example


Copyright 2023 © pythontic.com