Table of contents
1.
Introduction
2.
Dropdown List
3.
Implementation
3.1.
Basic Approach
3.1.1.
Python Code
3.1.2.
.kv Code
3.1.3.
Output
3.1.4.
Terminal Output
4.
Frequently Asked Questions
4.1.
Which is better: Flutter or Kivy?
4.2.
Is Kivy suitable for mobile apps?
4.3.
Is Kivy safe?
4.4.
Is Kivy free?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Dropdown List in Kivy

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

Introduction

Application development, Desktop, and Android are a significant part of python programming. That is where Kivy can be a handy tool for python developers. Kivy is a platform-independent GUI tool that you can run on Android, IOS, Linux, Windows, or any other operating system. Although we majorly utilize Kivy for Android development, its functionality is not limited. 

In this article, we will walk through the implementation of the Dropdown List in Kivy python using a .kv file with the help of a few examples. This article assumes that you have worked with Python programming. In case you are not sure of your knowledge of Python, you can check out our Basics of Python article and come back here.

Dropdown List

A dropdown list allows you to display a list of widgets under a widget that is displayed. The list of widgets can have any widget like simple buttons, images, etc which is unlike other toolkits. You can also use a dropdown list with custom widgets.

A dropdown list's positioning is entirely automatic, but you should always try to place the dropdown list so that the user can choose an item from the list.

Implementation

The first step in implementing the FloatLayout in Kivy is importing it. 

from kivy.uix.floatlayout import DropDown
You can also try this code with Online Python Compiler
Run Code

Basic Approach

After importing FloatLayout from Kivy, you can follow a Basic Approach given below:

  1. import kivy
  2. import kivy App
  3. import DropDown
  4. import Floatlayout(as per requirement)
  5. Set minimum version(optional)
  6. Create the Layout class
  7. Create the App class
  8. Create .kv file (same name as your app class; you can also name main.kv for MainApp class):
    1. create Dropdown
    2. create callback
    3. And many more styling as needed.
  9. Return Layout/widget/Class(as per requirement)
  10. Run an instance of the class
     

We will look at a sample python application code using Kivy to demonstrate the working of the approach mentioned above for the Dropdown list. The code creates a Button with a dropdown list.

Python Code

#dropdown.py


# import the kivy module    
import kivy
   
#
from kivy.app import App
   

kivy.require('1.9.0')
   

from kivy.uix. dropdown import DropDown
   

from kivy.uix.floatlayout import FloatLayout


from kivy. uix . button import Button

class CustomDropDown(DropDown):
    pass


class DropdownDemo(FloatLayout):
   
    def __init__(self, **kwargs):
       
        #the opening window button is created here,
        #not in kv
        super(DropdownDemo, self).__init__(**kwargs)
        self.dropdown = CustomDropDown()

        # Creating a self widget button
        self.mainbutton = Button(text ='Are you a Ninja Coder?',
                                size_hint_x = 0.8, size_hint_y = 0.15)
       
        # Adding button to FloatLayout
        self.add_widget(self.mainbutton)

        # Adding actions
        # If clicked
        self.mainbutton.bind(on_release = self.dropdown.open)

        # root.select on_select called
        self.dropdown.bind(on_select = lambda\
                        instance, x: setattr(self.mainbutton, 'text', x))
        self.dropdown.bind(on_select = self.callback)

    def callback(self, instance, x):
       
        print ( "The chosen mode is: {0}" . format ( x ) )


class MainApp(App):
   
    #The build function returns root,
 i.e, DropdownDemo ()
 
    #you can call root in the kv file only.
    def build(self):
        return DropdownDemo()


if __name__ == '__main__':
   
    MainApp().run()
You can also try this code with Online Python Compiler
Run Code

.kv Code

<CustomDropDown>:
    Button:
        text: 'Ninja Coders'
        size_hint_y: None
        height: 44
        on_release: root.select('Coding Ninja')
    Label:
        text: 'Not a Ninja Coder'
        size_hint_y: None
        height: 44
    Button:
        text: 'Happy Learning'
        size_hint_y: None
        height: 44
        on_release: root.select('CodingNinjas')

Output

Terminal Output

Frequently Asked Questions

Which is better: Flutter or Kivy?

There are a few reasons one can opt for either of the two tools.

  • Flutter has more ready-to-use components by default and provides more control over the pixels, making it easier to build a beautiful UI than Kivy.
  • The community support for Flutter is better than Kivy.
  • Flutter uses Dart, and Kivy uses Python.
  • Flutter performs better on UI rendering. Both Flutter and Kivy use GPU for tasks, so performance for computation should be comparable.
  • Flutter is faster and feels more natural because it compiles to native code that runs on dart VM; Kivy uses some bridge scheme.
     

Is Kivy suitable for mobile apps?

Kivy is an excellent choice if you want users to operate your mobile app on different devices and want or need its look and controls to be consistent.

 

Is Kivy safe?

The package is deemed safe to use. The Kivy package in Python was scanned for known vulnerabilities and missing licenses, and no issues were found.

 

Is Kivy free?

Kivy is a free and open-source Python framework for developing mobile apps and application software with a natural user interface (NUI).

Conclusion

In this article, we have extensively discussed the Float Layout in Kivy and its implementation in Python with the help of examples. There are many more Python frameworks and tools that you can learn and utilize for application development.

We hope that this blog has helped you enhance your knowledge of Kivy Python and if you would like to learn more, check out our articles on Basics of Python and Popular Python LibrariesDo upvote our blog to help other ninjas grow, and head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.

Happy Learning!

Live masterclass