Introduction
In this article, we will be covering Spinner Widget in Kivy. It is a Python using GUI tool that works on any platform provided by Kivy module python. We will also see how to use the .kv file in Kivy to develop the application's layout. This article assumes that you have been somewhat familiar with the Basics of Python otherwise it will be a bit difficult to understand it at first glance however you can still go through this article.
Spinner Widget in Kivy
A Spinner is a widget that allows you to select one value from a group of options quickly. A spinner's default state displays the value that is currently selected. Touching the spinner brings up a dropdown menu with all of the other options from which the user can choose.
A spinner object, like a combination box, can contain numerous values and one of them can be picked. The spinner object can have a callback attached to it to get notifications when a value is selected.
Implementation
To work with a spinner you must have to import:
from kivy.uix.spinner import Spinner
Below is the implementation of the Algorithm
1 import kivyApp
2 import spinner
3 import Floatlayout(according to need)
4. Create Layout class
4.1 define the clicked function in it
5. Create App class
6. create .kv file (name same as the app class)
6.1 create Spinner
6.2 create callback
6.4 And many more styling as needed
7. return Layout/widget/Class(according to requirement)
8. Run an instance of the class
Python Code
from kivy.base import runTouchApp
from kivy.uix.spinner import Spinner
spinner = Spinner(
# default value shown
text='Choose',
# available values
values=('Python', 'C++', 'Java', 'Others'),
# just for positioning in our example
size_hint=(None, None),
size=(100, 44),
pos_hint={'center_x': .5, 'center_y': .5})
def show_selected_value(spinner, text):
print('The spinner', spinner, 'has text', text)
spinner.bind(text=show_selected_value)
runTouchApp(spinner)
Kivy File
# Creating the Layout i.e root of the Layout class
FloatLayout:
Spinner:
size_hint: None, None
size: 100, 44
pos_hint: {'center': (.5, .5)}
text: 'Choose'
values: 'Java', 'C++', 'Python', 'Others'
on_text:
print("The spinner {} has text {}".format(self, self.text))