Introduction
Kivy is a Python platform-independent GUI tool. Because it can run on Android, iOS, Linux, and Windows, among other platforms, it is mainly used to construct Android applications, but it may also be used to develop desktop programs.
A flexible drop-down list that can be used in conjunction with custom widgets. It enables you to show a list of widgets beneath a shown widget. In contrast to other toolkits, the list of widgets can include any form of widget: simple buttons, photos, etc.
The positioning of the drop-down list is entirely automatic: we will always strive to arrange the drop-down list so that the user can select an item from the list.
Let’s learn about the drop-down list in detail.
Drop-down List
Custom widgets can make use of a drop-down list. It enables you to show a list of widgets beneath a shown widget. In contrast to other toolkits, the list of widgets can include any form of widget: simple buttons, photos, etc. The positioning of the drop-down list is entirely automatic: we will always strive to arrange the drop-down list so that the user can select an item from the list. Some things to keep in mind when creating a drop-down list:
- When adding widgets, we must specify the height manually (by disabling the size_hint_y) so that the drop-down can calculate the area required.
- All of the buttons in the drop-down list will activate the drop-down DropDown.select() method. The main button text will display the drop-down selection after it has been called.
Approach
A Basic Approach to creating drop-down in kivy :
1) import kivy
2) import kivy App
3) import the drop-down list
4) import the button
5) import the runTouchApp
6) We will create the drop-down
7) Create the runtouchApp method, which accepts a widget as an argument and uses it to run the App.
Implementation
Now let’s move on to the implementation of the above approach.
# Importing the kivy module
import kivy
from kivy.app import App
# This restricts the minimum required kivy version
kivy.require('1.9.0')
# Importing Drop-down to use in the program from the module
from kivy.uix. drop-down import DropDown
# The Button is a Label with associated actions that is triggered when the button is pressed(or released after a click/touch)
from kivy.uix.button import Button
# Another way used to run kivy App
from kivy.base import runTouchApp
# Create a dropdown with 10 buttons
dropdown = DropDown()
for index in range(10):
# Adding button
btn = Button(text ='List % d' % index, size_hint_y = None, height = 30)
# Binding the button for showing the text
btn.bind(on_release = lambda btn: dropdown.select(btn.text))
# Then adding the button inside the drop-down
dropdown.add_widget(btn)
# Creating the big main button
main_button = Button(text ='Hello', size_hint =(None, None), pos =(350, 300))
# When the main button is released, the drop-down menu appears.
# Take note that all bind() calls pass the caller's instance
# (here, the main_button instance) as the first argument to the callback (here, drop-down.open.).
main_button.bind(on_release = dropdown.open)
dropdown.bind(on_select = lambda instance, x: setattr(main_button, 'text', x))
# runtouchApp:
# If you only pass a widget to runtouchApp(),
# A Window will be created and your widget will be added as the root widget.
runTouchApp(main_button)
Output
And after clicking on the main button, the dropdown list will open:
Let’s move on to Frequently asked questions.