Open In App
Related Articles

Python | Drop-down list in kivy using .kv file

Improve Article
Save Article
Like Article

Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, linux and Windows etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications.

👉🏽 Kivy Tutorial – Learn Kivy with Examples.

Dropdown list

A drop-down list can be used with custom widgets. It allows you to display a list of widgets under a displayed widget. Unlike other toolkits, the list of widgets can contain any type of widget: simple buttons, images etc.
The positioning of the drop-down list is fully automatic: we will always try to place the dropdown list in a way that the user can select an item in the list.

To work with this widget you must have to import:
from kivy.uix.dropdown import DropDown

Basic Approach:
1) import kivy
2) import kivyApp
3) import dropdown
4) import Floatlayout(according to need)
5) Set minimum version(optional)
6) Create Layout class
7) Create App class
9) create .kv file (name same as the app class):
        1) create Dropdown
        2) create callback
        3) And many more styling as needed
10) return Layout/widget/Class(according to requirement)
11) Run an instance of the class file


Code of How to use drop-down list with.kv file
# Program to Show how to create a switch 
# import kivy module    
import kivy  
# base Class of your App inherits from the App class.    
# app:always refers to the instance of your application   
from import App 
# this restrict the kivy version i.e  
# below this kivy version you cannot  
# use the app or software  
# drop-down menu is a list of items that
# appear whenever a piece of text or a
# button is clicked.
# To use drop down you must have ti import it
from kivy.uix.dropdown  import DropDown
# module consists the floatlayout  
# to work with FloatLayout first  
# you have to import it  
from kivy.uix.floatlayout import FloatLayout
# The Button is a Label with associated actions that
# are triggered when the button is pressed (
# or released after a click / touch).
from kivy. uix . button  import Button
class CustomDropDown(DropDown):
class DropdownDemo(FloatLayout):
    '''The code of the application itself.''' 
    def __init__(self, **kwargs):
        '''The button at the opening of the window is created here,
        not in kv
        super(DropdownDemo, self).__init__(**kwargs)
        self.dropdown = CustomDropDown()
        # Creating a self widget button
        self.mainbutton = Button(text ='Do you in college?',
                                 size_hint_x = 0.6, size_hint_y = 0.15)
        # Added button to FloatLayout so inherits this class 
        # Adding actions 
        # If click 
        self.mainbutton.bind(on_release =
        # 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):
        '''x is self.mainbutton.text refreshed''' 
        print ( "The chosen mode is: {0}" . format ( x ) )
class MainApp(App):
    '''The build function returns root,
    here root = DropdownDemo ().
    root can only be called in the kv file.
    def build(self):
        return DropdownDemo()
if __name__ == '__main__':

.kv file:


        text: 'College Name'
        size_hint_y: None
        height: 44
        on_release:'College is')
        text: 'Not in college'
        size_hint_y: None
        height: 44
        text: 'KccItm'
        size_hint_y: None
        height: 44


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 08 Dec, 2021
Like Article
Save Article
Similar Reads
Complete Tutorials