Table of contents
1.
Introduction
2.
Slider Widget
2.1.
Output
3.
FAQs
3.1.
What is a widget in Python Kivy?
3.2.
 
3.3.
What is the modal view in Kivy?
3.4.
What is the relative layout in Kivy?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Slider Widget in Kivy

Author SHIVANGI MALL
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Kivy is a multi-platform Python GUI development library that runs on iOS, Android, Windows, OS X, and GNU/Linux. It aids in the development of apps that make use of cutting-edge multi-touch user interfaces. Kivy's core concept allows developers to create an app once and deploy it across all devices, making code reusable and deployable and enabling quick and easy interaction design and prototyping.

Slider Widget

The Slider widget has the appearance of a scrollbar. The Slider widget appears to be similar to the one we use in Android to adjust brightness, volume, and other settings.

  • For app development, Kivy includes a slider widget.
     
  • A Kivy slider widget can be used in either a vertical or horizontal orientation in an app.
     
  • The slider control can provide a minimum, maximum, and default value.
     
  • Kivy has many slider widget settings for changing the cursor, cursor picture, border, background, and a region between the minimum and maximum values in different orientations.
     
  • Kivy additionally allows you to work with normalized range values rather than the real range provided by the slider.

 

  • A normalized value is one in which the smallest value is 0, and the greatest value is 1.

 

To use the slider, you must first import the module kivy.uix.slider, which contains all of the slider's features and functions.

The following is a basic approach to use when designing a slider:

import kivy 
import kivy App 
import gridlayout(according to need, not compulsory) 
import Label(according to need, not compulsory) 
import Slider 
import Numeric property 
set minimum version(optional) 
Extend the class 
Add and return a widget 
Run an instance of the class
You can also try this code with Online Python Compiler
Run Code

The code for implementing the slider is as follows:

# to import kivy
import kivy
from kivy.app import App

# we use GridLayout to arrange children in a matrix.
from kivy.uix.gridlayout import GridLayout

# It will through an error
# If we will not import slider
from kivy.uix.slider import Slider

# Label widget is used for rendering text.
from kivy.uix.label import Label
from kivy.properties  import NumericProperty

 
# class to defining the sliders
class WidgetContainer(GridLayout):


    def __init__(self, **kwargs):

        super(WidgetContainer, self).__init__(**kwargs)


        # 2 columns in a grid layout

        self.cols = 2

        self.brightnessControl = Slider(min=0, max =100)

        # 1st row - one label, one slider    

        self.add_widget(Label(text='brightness'))

        self.add_widget(self.brightnessControl)

        # 2nd row - one label for caption, one label for slider value

        self.add_widget(Label(text='Slider Value'))

        self.brightnessValue = Label(text='0')

        self.add_widget(self.brightnessValue)

        # On the slider object, Attach a callback for the attribute named value

        self.brightnessControl.bind(value=self.on_value)

 
    def on_value(self, instance, brightness):

        self.brightnessValue.text = "%d"%brightness


# The app class

class Slider_Example(App):

    def build(self):

        widgetContainer = WidgetContainer()

        return widgetContainer
 

# Run the app      

if __name__ == '__main__':

    Slider_Example().run()
You can also try this code with Online Python Compiler
Run Code

Output

FAQs

What is a widget in Python Kivy?

In Kivy, a Widget is the fundamental component of a GUI interface. It comes with a Canvas that you may use to draw on the screen. It receives and responds to events.

Kivy's widgets are arranged in a tree. Your program has a root widget, which normally has children who can have their own children. The children attribute, a Kivy ListProperty, is used to represent the children of a widget.

 

What is the modal view in Kivy?

Modal views are created with the ModalView widget. The view will by default, cover the whole "primary" window.

Remember that a Widget's default size is size hint=. (1, 1). We use size_hints with values lower than 1 (for example, size_hint=(.8,.8)) or disable the size_hint and use fixed-size attributes if you don't want your view to be fullscreen.
 

What is the relative layout in Kivy?

We can use a relative layout to set relative coordinates for children. Use the FloatLayout if you want absolute placement. The RelativeLayout class is similar to the FloatLayout class, except that its child widgets are positioned relative to the layout. 

This layout works similarly to FloatLayout, with the exception that the positioning properties (pos, x, center x, right, y, center y, and top) are related to the Layout size rather than the window size.

Conclusion

In this article, we have extensively discussed the slider widget in kivy.

We hope that this blog has aided you in broadening your horizons. If you want to learn more, check out our articles on Application DevelopmentModules & Packages in Python, and Best Python Projects. Do upvote our blog to help other ninjas grow.

 

Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!
 

Happy Reading!

 

Live masterclass