Table of contents
1.
Introduction
2.
Drop-down List
2.1.
Approach
2.2.
Implementation
2.2.1.
Output
3.
Frequently Asked Questions
3.1.
What is kivy?
3.2.
What extension is used for kivy files?
3.3.
Why do we use drop-down?
3.4.
What is the necessary condition while adding widgets?
3.5.
What will happen if you only pass a widget to runtouchApp()?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

The drop-down List In Kivy

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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)
You can also try this code with Online Python Compiler
Run Code

Output

 

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

Let’s move on to Frequently asked questions.

Frequently Asked Questions

What is kivy?

Kivy is a Python platform-independent Graphics User Interface(GUI) tool.

What extension is used for kivy files?

The Kivy files are saved with the .ky file extension.

Why do we use drop-down?

It enables us 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, and so on.

What is the necessary condition while adding widgets?

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.

What will happen if you only pass a widget to runtouchApp()?

A window will be created, and your widget will be added as the root widget.

Conclusion

This article extensively discussed the drop-down list in kivy. We learned the concept of drop-down lists and why they are used. We learned the basic approach and the implementation of creating the drop-down list in Python.

After reading about The drop-down List In Kivy are you not feeling excited to read/explore more articles on the topics of kivy? Don’t worry, Coding ninjas has you covered. To learn more on such topics, you can visit StackLayout in Kivy using .kv fileFloatLayout in Kivy using .kv fileBoxLayout Widget in KivyThe drop-down List In Kivy, and Background template in Kivy!

Refer to our guided path on Coding Ninjas Studio to upskill yourself in Data structure and algorithmsCompetitive Programming, JavascriptSystem Design, and many more if you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others.

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass