In this Kivy Python tutorial we want to learn about How to Create Slider in Python Kivy, so Kivy is an open source Python library for creating graphical user interfaces or GUIs that are cross platform and can run on different operating systems such as Windows, macOS and Linux. sliders are used to allows user for inputting data within a range.
First of all we need to install Kivy and we can use pip for that.
1 |
pip install kivy |
After that we need to import the necessary modules to create the slider. in this example we will be using Kivy App class and the Kivy Slider class.
1 2 3 |
import kivy from kivy.app import App from kivy.uix.slider import Slider |
Now we can create a class for our slider. this class will inherit from the Kivy App class, and it provides a base class for creating Kivy applications. in this class we want to create an instance of the Kivy Slider class, and it will be used to create our slider.
1 2 3 4 |
class SliderApp(App): def build(self): slider = Slider(min=0, max=100, value=50) return slider |
Now that we have created our SliderApp class, we can run the application by adding the following code to our Python file:
1 2 |
if __name__ == '__main__': SliderApp().run() |
We can also customize the appearance and behavior of the slider by setting different attributes of the Slider class. for example we can set the size of the slider by setting the size_hint attribute, and we can set the orientation of the slider by setting the orientation attribute.
1 2 3 4 |
class SliderApp(App): def build(self): slider = Slider(min=0, max=100, value=50, size_hint=(0.5, 0.1), orientation='horizontal') return slider |
This is the complete code for this article
1 2 3 4 5 6 7 8 9 10 |
from kivy.app import App from kivy.uix.slider import Slider class SliderApp(App): def build(self): slider = Slider(min=0, max=100, value=50) return slider if __name__ == '__main__': SliderApp().run() |
Run the complete code and this will be the result
