Change language

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:

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

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically