In this Kivy Tutorial we want to learn that 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.
How to Install Kivy?
First of all we need to install Kivy and we can use pip for that, open your command prompt or terminal and write this command.
1 |
pip install kivy |
After that we need to import the necessary modules to create the slider. in this example we are using Kivy App class and 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, you can set 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 11 12 13 14 15 16 17 18 |
# Import the App class from kivy.app import App # Import the Slider class from kivy.uix.slider import Slider class SliderApp(App): # Override the build method def build(self): # Create a new Slider widget slider = Slider(min=0, max=100, value=50) # Return the slider widget return slider if __name__ == '__main__': # Run the Kivy application SliderApp().run() |
Run the complete code and this will be the result
Subscribe and Get Free Video Courses & Articles in your Email