Saturday, November 16, 2024
Google search engine
HomeLanguagesPython | Spinner widget in Kivy using .kv file

Python | Spinner widget in Kivy using .kv file

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.

Spinner widget :

To work with spinner you must have to import:

from kivy.uix.spinner import Spinner

Spinner is a widget that provides a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all the other available values from which the user can select a new one.
Like a combo box, a spinner object can have multiple values and one of the values can be selected.
A callback can be attached to the spinner object to receive notifications on selection of a value from the spinner object.

Basic Approach:

1) import kivy
2) import kivyApp
3) import spinner
4) import Floatlayout(according to need)
5) import window(optional)
6) Set minimum version(optional)
7) Create Layout class:
      define the clicked function in it
8) Create App class
9) create .kv file (name same as the app class):
        1) create Spinner
        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

Below is the implementation :

In the below code we have created the spinner and done the sizing and positioning and also attached the callback to the values.

.py file:

Python3




# Sample spinner app in kivy using .kv file
  
# to change the kivy default settings we use this module config
from kivy.config import Config
  
# 0 being off 1 being on as in true / false
# you can use 0 or 1 && True or False
Config.set('graphics', 'resizable', True)
  
# 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 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')
  
# Spinner is a widget that provides a
# quick way to select one value from a set.
# like a dropdown list
from kivy.uix.spinner import Spinner
  
# module consists the floatlayout 
# to work with FloatLayout first 
# you have to import it 
from kivy.uix.floatlayout import FloatLayout
  
# Here for providing colour to the background
from kivy.core.window import Window
  
  
# create LayoutClass
class SampBoxLayout(FloatLayout):
    # For Spinner defining spinner clicked function
    def spinner_clicked(self, value):
        print("Language selected is " + value)
   
  
# # Make an App by deriving from the App class
class SampleApp(App):
    def build(self):
   
        # Set the background color for the window
        Window.clearcolor = (0.555, 0.261, .888, 0.5)
        return SampBoxLayout()
  
# create object for the Appclass
root = SampleApp()
# run the class
root.run()
  


 
.kv file of the code:

Python3




# .kv file implementation of the .py file
  
# Creating the Layout i.e root of the Layout class
<SampBoxLayout>:
  
    # creating the spinner
    Spinner:
        # Assigning id 
        id: spinner_id
  
        # Callback 
        on_text: root.spinner_clicked(spinner_id.text)
  
        # initially text on spinner
        text: "Python"
  
        # total values on spinner
        values: ["Python", "Java", "C++", "C", "C#", "PHP"]
  
        # declaring size of the spinner
        # and the position of it
        size_hint: None, None
        size: 200, 50
        pos_hint:{'center_x':.5, 'top': 1}


Output:

Image 1:

Image 2:

Image 3:

Below is the output in video to get better understanding:

Last Updated :
26 Oct, 2021
Like Article
Save Article

<!–

–>

Similar Reads
Related Tutorials
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments