Python | Dropdown list in kiw using .kv file



Kivy — it is a platform independent GUI tool in Python. Since it can run on Android, IOS, Linux, Windows, etc. It is mainly used to develop Android application, but that does not mean that it cannot be used in desktop applications.

Dropdown

Dropdown can be used with custom widgets. Allows you to display a list of widgets below the displayed widget. Unlike other toolboxes, the widget list can contain any type of widget: simple buttons, images, etc.
The location of the dropdown is fully automatic: we will always try to position the dropdown so that the user can select an item in 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 

# .py file

“” “
Code How to use the drop-down list with a .kv file
"" "

  
# Program to show how to create a radio button
# import cool module

import kivy 

 
# Your application base class inherits from the application class
# app: always refers to your application instance

from kivy.app import App 

  
# this limits the kivy version ie
# below this version you can`t
# use application or software

kivy.require ( `1.9.0`

  
# the drop-down menu is a list of items that
# appear whenever a piece of text or
The # button is pressed.
# To use the dropdown you must import it

from kivy.uix.dropdown  import DropDown

 
# the module consists of floatlayout
# work with FloatLayout first
# you must import it < / code>

from kivy.uix.floatlayout import FloatLayout

 
# Button is a Label with associated actions that
# are triggered when the button is clicked (
# or issued after click / touch).

from kivy. uix. button  import Button

  

class CustomDropDown (DropDown):

  pass

  

 

class DropdownDemo (FloatLayout):

& # 39; & # 39; & # 39; The code of the application itself. & # 39; & # 39; & # 39;  

def __ init __ ( self , * * kwargs):

 

& # 39; & # 39; & # 39; This creates a button when opening a window,

not in square

"" " 

  super (DropdownDemo, self ) .__ init __ ( * * kwargs)

self . dropdown = CustomDropDown ()

 

# Create a bud widget

self . mainbutton = Button (text = `Do you in college?` ,

  size_hint_x = 0.6 , size_hint_y = 0.15 )

  

# Added a button to FloatLayout, so inherits this class

self . add_widget ( self .mainbutton)

 

# Add actions

# If you press

  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):

& # 39; & # 39; & # 39; x is self.mainbutton.text updated & # 39; & # 39; & # 39;  

print ( " The chosen mode is: {0} " . format (x))

 

 

class MainApp (App):

& # 39; & # 39; & # 39; 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__` :

  

  MainApp (). run ()

.kv file:

& lt; CustomDropDown & gt ;:

Button:

text: ` College Name`

size_hint_y: None

  height: 44

on_release: root.select ( `College is` )

Label:

  text: `Not in college`

size_hint_y: None

height: 44

Button:

  text: `KccItm`

  size_hint_y: None

height: 44

on_release: root.select ( `Kcc` )

Exit :