Table of contents
1.
Introduction
2.
Spinner Widget
3.
Implementation Approach
4.
Implementation Code
5.
Code Explanation
5.1.
Output
5.2.
Output Image 1
5.3.
Output Image 2
5.4.
Output Image 3
6.
Frequently Asked Questions
6.1.
What is a widget?
6.2.
What is a Label?
6.3.
What are the different types of Layouts?
6.4.
What is the extension of Kivy files?
7.
Conclusion
Last Updated: Mar 27, 2024

Spinner Widget in Kivy

Author Pankhuri Goel
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Kivy is a GUI open-source Python library that lets you create cross-platform programmes for Windows, macOS, Android, iOS, Linux, and Raspberry Pi. It assists in the creation of apps with cutting-edge multi-touch user interfaces open-source.

To use the Spinner widget, you must first import the following:

from kivy.uix.spinner import Spinner

Let us see more about the Spinner widget now.

Spinner Widget

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 drop-down menu with all the other options from which the user can choose.

A spinner object, like a combo box, can contain multiple values, and one of them can be chosen. The spinner object can have a callback attached to it to get notifications when a value is selected.

Implementation Approach

The basic approach one should follow while implementing the Spinner widget using Kivy is as follows:

Step 1: import libraries, classes, widgets and layouts required (in this case, kivy, kivyApp, Label, Spinner, Floatlayout)

Step 2: create the required class (in this case, App class) 

Step 3: create a spinner in the class; attach labels and a callback to it

Step 4: return the layout/widget/class (in this case, layout)

Step 5: run the instance of the class (in this case, SpinnerWidget())

Implementation Code

Given below is the code showing the implementation of the Spinner Widget.

# Simple Spinner Widget Implementation
from kivy.config import Config

# 0 being off 1 being on as in true or false
# you can use 0 or 1 OR True or False
Config.set('graphics', 'resizable', True)

import kivy
from kivy.app import App

# restrict the kivy version(optional)
kivy.require('1.9.0')

from kivy.uix.label import Label
from kivy.uix.spinner import Spinner
from kivy.uix.floatlayout import FloatLayout

# Make an App by deriving it from the App class
class SpinnerWidget(App):

	# defining build
	def build(self):

		# creating floatlayout layout
		layout = FloatLayout()

		# creation of the spinner
		# configuration of spinner object and addition to layout
		self.spinnerObject = Spinner(text ="Widgets in Kivy",
		values =("Label", "Button", "CheckBox", "Image", "Slider", "Switch", "Progress Bar", "Text Input", "Toggle Button"),
		background_color =(0.787, 0.445, 0.212, 1))

		self.spinnerObject.size_hint = (0.4, 0.2)

		self.spinnerObject.pos_hint ={'x': .40, 'y':.80}

		layout.add_widget(self.spinnerObject)

		return layout


# Run the app
if __name__ == '__main__':
	SpinnerWidget().run() 

Code Explanation

In the above code, we first import the required libraries, classes, widgets and layouts. After that, we create the SpinnerWidget() class. In this case, we first create an instance of Float Layout and then go on to create the spinner widget. We configure the spinner widget and define the various texts in the drop-down list. We add the callback and layout in the spinner and then return the layout. Lastly, we run an instance of the SpinnerWidget() class.

When we click on any item in the list, the chosen item will send a callback, which will be displayed at the top of the Spinner object.

Output

Output Image 1

Output Image 2

Output Image 3

Frequently Asked Questions

What is a widget?

Kivy Widgets are used to create graphical user interfaces in Kivy. Labels, Canvas, drop-down menus, and many more widgets are available in Kivy.

 

What is a Label?

The Label widget is used to display text. It can handle both ASCII and Unicode strings.

 

What are the different types of Layouts?

The various types of Layouts available in Kivy are as follows:

→ Grid Layout

→ Box Layout

→ Float Layout

→ Anchor Layout

→ Stack Layout

 

What is the extension of Kivy files?

The extension used with Kivy files is .kv.

Conclusion

In this article, we learned about the spinner widget in Kivy. We saw the various steps required to implement it. We repeated these steps and used them to make a Python Spinner widget.

We hope this blog has helped you enhance your knowledge. If you want to learn more, check out our articles on Introduction to Kivy - Coding Ninjas Coding Ninjas StudioKivy Float Layout in Python - Coding Ninjas Coding Ninjas StudioMultiple Layout - Coding Ninjas Coding Ninjas Studio and Shaping The Button - Coding Ninjas Coding Ninjas Studio. Do upvote our blog to help other ninjas grow.

Head over to our practice platform Coding Ninjas Studio to practice top problems, follow guided paths, attempt mock tests, read interview experiencesinterview bundle, solve problems, participate in contests and much more!

Happy Reading!

Live masterclass