The dropdown list is used with custom widgets. The users use the dropdown list for displaying the list of the widgets that are under the displayed widgets. The list of widgets is used for storing any type of widgets, such as images, simple buttons, and many more.
The users can rearrange the position of the dropdown list as it is fully automatic. Furthermore, the developer can place this list in simple ways so that the users can easily select the items from the list.
Important points to remember while developing a dropdown list:
When a user adds the widgets, the user should specify their heights manually, so the dropdown can easily calculate the area it would be needing.
All the buttons inside the dropdown list should have to trigger the dropdown “DropDown.select()” After it is called, the main button text has to be displayed in the selection of the dropdown.
For working with this widget, the developer should import the following first:
from kivy.uix.dropdown import DropDown
Basic approach for the Dropdown list:
Import the Kivy
Then, import the Kivy app
Then, we will import the dropdown list
And, import the button
We can also see its minimum version, which is optional.
At last, we will import the runTouchApp
We will create the dropdown
Then, we will create the runTouchApp function, which will take the widget as an argument for running the Application.
Example:
# Python Program for explaining how to create a drop-down in kivy
# first we will import kivy module
import kivy
# the base Class of our Application is inherited from the App class.
# app is alway used for refering to the instance of our application
from kivy.app import App
# this will restrict the kivy version that means we can not use
# the application or software below this Version of Kivy
kivy.require('1.9.0')
# Now we will import the Drop-down from the module for using it in the program
from kivy.uix.dropdown import DropDown
# The Button will be a Label with associated actions which will be released when the button is clicked
from kivy.uix.button import Button
# the another way used for running the kivy application
from kivy.base import runTouchApp
# now we will create the dropdown with 15 buttons
drop_down = DropDown()
for index in range(15):
# now, Add the button in the drop down list
btton = Button(text ='List % d' % index, size_hint_y = None, height = 30)
# now we will bind the button for showing the text when it is selected
btton.bind(on_release = lambda btton: drop_down.select(btton.text))
# then we will add the button inside the drop_down list
drop_down.add_widget(btton)
# now we will create the big main button
main_button = Button(text ='MAIN', size_hint =(None, None), pos =(350, 300))
# now, we will first show the drop_down menu when the main button will releases
# we should note that all of the bind() function calls will pass the instance of the caller
# as the first argument of the callback (in this program, the main_button instance)
# now, dropdown.open.
main_button.bind(on_release = drop_down.open)
# now we have to do last thing, listen for the selection in the
# dropdown list and assign the data to the button text.
drop_down.bind(on_select = lambda instance, x: setattr(main_button, 'text', x))
# runtouchApp:
# If we pass only the widget in runtouchApp(), the Window will be
# created and our widget will be added to that window as the root widget.
runTouchApp(main_button)
Output:
After running the above code, the user will get this output:

And, after clicking on the main button, the dropdown list will open:

Leave a Review