Change language

Python | Spinner widget in kiwi

|

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.

Spinner widget:

To work with a spinner, you need to import:

 from kivy.uix.spinner import Spinner 

Spinner — it is a widget that provides a quick way to select one value from a set. In the default state, the counter shows its currently selected value. Touching the counter displays a drop-down menu with all other available values ​​from which the user can select a new one.
As in the combo box, the counter object can have multiple values, and you can select one of the values.
A callback can be attached to the spinner object to receive notifications about the selection of a value from the spinner object.

  Basic Approach:  1) import kivy 2) import kivyApp 3) import Label 4) import Spinner 5) import Floatlayout 6) Set minimum version (optional) 7) create App class: 1) Create the spinner 2) Attach the labels to spinners 3) Attach a callback also 8) return Layout / widget / Class ( according to requirement) 9) Run an instance of the class 

Simple spinner implementation:

# Example Spinner apps in kiv to change
# kivy default settings we use this module config

from kivy.config import Config

  
# 0 disabled 1 enabled as true / false
# You can use 0 or 1 & amp; & amp; True or False

Config. set ( ’graphics’ , ’ resizable’ , True )

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

import kivy 

 
# Your application base class inherits from the application class.
# app: always refers to an instance of your applications

from kivy.app import App

  
# this limits the kivy version ie
# below this version you cannot
# use the application or software

kivy.require ( ’1.9.0’ )

  
# The Label widget is designed to render text.

from kivy.uix.label import Label

 
# Spinner is a widget that provides
# a quick way to choose one value from the set.
# as a drop-down list

from kivy.uix.s pinner import Spinner

  
# the module consists of floatlayout
# work with FloatLayout first
# you must import it

from kivy.uix.floatlayout import FloatLayout

 

 
# Create an application that derives from the App class

class SpinnerExample (App):

 

# define assembly

def build ( self ):

  

  # create floatlayout

layout = FloatLayout ()

 

# create a spinner

  # customize the spinner and add to the layout

self . spinnerObject = Spinner (text = "Python" ,

values = ( "Python" , " Java " , " C++ " , "C" , "C #" , "PHP" ),

background_color = ( 0.784 , 0.443 , 0.216 , 1 )) 

 

self . spinnerObject.size_hint = ( 0.3 , 0.2 )

  

self . spinnerObject.pos_hint = { ’x’ : . 35 , ’y’ :. 75 }

 

  layout.add_widget ( self . spinnerObject)

 

# return layout

return layout;

 

 
# Run the application

if __ name__ = = ’__main__’ :

SpinnerExample (). run () 

Exit:

Image 1:

Image 2:

Now, if we need to tell the user every time which item in the list is selected, we will display a label next to the spinner that says about the selected label. We will also print out the value and text of the counter.

Below is the implementation:

Exit :

Image 1:

Image 2:

Below is the output in the video for better understanding:

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


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

# Spinner application example in kiw to change
# kivy default settings we use this module config

from kivy.config import Config

 
# 0 disabled 1 enabled as true / false
# You can use 0 or 1 & amp; & amp; True or False

Config. set ( ’graphics’ , ’ resizable’ , True )

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

import kivy 

 
# Your application base class inherits from the application class.
# app: always refers to an instance of your applications

from kivy.app import App

  
# this limits the kivy version ie
# below this version you cannot
# use the application or software

kivy.require ( ’1.9.0’ )

  
# The Label widget is designed to render text.

from kivy.uix.label import Label

 
# Spinner is a widget that provides
# a quick way to choose one value from the set.
# as a drop-down list

from kivy.uix.s pinner import Spinner

  
# the module consists of floatlayout
# work with FloatLayout first
# you must import it

from kivy.uix.floatlayout import FloatLayout

 

 
# Create an application that derives from the App class

class SpinnerExample (App):

 

# define assembly

def build ( self ):

  

  # create floatlayout

layout = FloatLayout ()

 

# create a spinner

  # customize the spinner and add to the layout

self . spinnerObject = Spinner (text = "Python" ,

values = ( "Python" , " Java " , " C++ " , "C" , "C #" , "PHP" ),

background_color = ( 0.784 , 0.443 , 0.216 , 1 )) 

 

self . spinnerObject.size_hint = ( 0.3 , 0.2 )

  

self . spinnerObject.pos_hint = { ’x’ : . 35 , ’y’ :. 75 }

 

  layout.add_widget ( self . spinnerObject)

self . spinnerObject.bind (text = self . on_spinner_select)

 

# This also changes the tag information

# add a label showing highlighting from the spinner

  self . spinnerSelection = Label (text = "Selected value in spinner is:% s"  

% self .spinnerObject.text)

  

  layout.add_widget ( self . spinnerSelection)

self . spinnerSelection.pos_hint = { ’x’ :. 1 , ’y’ :. 3 }

 

return layout;

 

# call back for selection in counter object

  def on_spinner_select ( self , spinner, text):

self . spinnerSelection.text = "Selected value in spinner is:% s"

% self . spinnerObject.text)

  

  print ( ’The spinner’ , spinner, ’ have text’ , text)

 

 
# Run the application

if __ name__ = = ’__main__’ :

SpinnerExample (). run ()