Python | Dropdown list in kivy

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.

Dropdown list

A dop-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.



Some important points to keep in mind while making a drop-down list:

  • When adding widgets, we need to specify the height manually (disabling the size_hint_y) so the dropdown can calculate the area it needs.
  • All the buttons within the dropdown list will trigger the dropdown DropDown.select() method. After being called, the main button text will display the selection of the dropdown.
  • To work with this widget you must have to import:

    from kivy.uix.dropdown import DropDown

    Basic Approach:
    1) import kivy
    2) import kivy App
    3) import dropdown list
    4) import button
    5) set minimum version(optional)
    6) import runTouchApp
    7) Create dropdown
    8) create runtouchApp method 
       which takes widget as an argument
       to run the App

    Implementation of the Approach –

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Program to explain how to creat drop-down in kivy 
         
    # 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 kivy.app import App 
         
    # this restrict the kivy version i.e  
    # below this kivy version you cannot  
    # use the app or software  
    kivy.require('1.9.0'
         
    # Importing Drop-down from the module to use in the program
    from kivy.uix.dropdown import DropDown
      
    # 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
      
    # 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 in drop down list
        btn = Button(text ='Value % d' % index, size_hint_y = None, height = 40)
      
        # binding the button to show the text when selected
        btn.bind(on_release = lambda btn: dropdown.select(btn.text))
      
        # then add the button inside the dropdown
        dropdown.add_widget(btn)
      
    # create a big main button
    mainbutton = Button(text ='Hello', size_hint =(None, None), pos =(350, 300))
      
    # show the dropdown menu when the main button is released
    # note: all the bind() calls pass the instance of the caller 
    # (here, the mainbutton instance) as the first argument of the callback
    # (here, dropdown.open.).
    mainbutton.bind(on_release = dropdown.open)
      
    # one last thing, listen for the selection in the 
    # dropdown list and assign the data to the button text.
    dropdown.bind(on_select = lambda instance, x: setattr(mainbutton, 'text', x))
      
    # runtouchApp:
    # If you pass only a widget in runtouchApp(), a Window will
    # be created and your widget will be added to the window
    # as the root widget.
    runTouchApp(mainbutton)

    chevron_right

    
    

    Output:

    Image 1:

    Image 2:



    My Personal Notes arrow_drop_up

    Competative Programmer

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




    Article Tags :

    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.