Python | Slider widget using .kv file



Slider:
To work with the slider, you first need to import the module, which consists of all functions, functions of the slider, i.e.

  Module:  kivy.uix.slider 

Basic approach to follow when creating Slider —

 1) import kivy 2) import kivyApp 3) import BoxLayout 4) set minimum version (optional) 5) Extend the class 6) set up .kv file (name same as the Slider.kv) 7) Run an instance of the class 

Below is the code that implements slider with .kv file:

# main.py slider file

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

from kivy.app import App

 
# BoxLayout places children in a vertical or horizontal border.
# or help put the kids in the right place.

from kivy.uix.boxlayout import BoxLayout 

 

 
# create the root widget used in the .kv file

class SliderWidget (BoxLayout):

pass

 
# class where the name of the .kv file should be called Slider.kv.
# or create an application class

class Slider (App):

def build ( self ):

# return a SliderWidget instance

return SliderWidget ()

 
# run application

if __ name__ = = `__main__` :

Slider (). run ()

Now file Slider.kv : file Slider.kv

& lt; SliderWidget & gt ;:

 

# create a slider

Slider:

 

# giving the slider orientation

orientation: "vertical"

min : 0   # minimum Slider value

max : 100 # maximum Slider value

value: 0   # initial Slider value

 

# when the slider is moved to increase the value

on_value: label1.text = str ( int ( self . value))

 

# Add shortcut

  Label:

  id : label1

font_size: "30sp"

text: "0"

color: 1 , 0 , 0 , 1

 

Slider:

orientation: "horizontal"

min : 0

max : 100

value: 30

  on_value: label2.text = str ( int ( self . value))

 

 

Label:

  id : label2

  font_size: "30sp"

text: "30"

color: 0 , 0 , 1 , 1

Output:

For a slider without a .kv file, see — Python | Slider widget in Kiwi