In this Python PyQt6 article we want to learn How to Create Slider in Python PyQt6, so Sliders are an essential component of modern graphical user interfaces, because it allows users to input and manipulate numerical values with easy. also, PyQt6 is one of the popular frameworks, that you can build GUI applications with Python.
First of all you need to install PyQt6, we can use pip for PyQt6 installation.
1 |
pip install PyQt6 |
After installation we need to import required classes and modules from PyQt6.
1 2 3 |
from PyQt6.QtWidgets import QApplication, QMainWindow, QSlider, QLabel, QVBoxLayout, QWidget from PyQt6.QtCore import Qt import sys |
After that we create an application instance and a main window, which will serve as the container for our slider.
1 2 3 |
app = QApplication(sys.argv) window = QMainWindow() window.setWindowTitle("Slider Example") |
Now, let’s create the slider itself. for that we want to use the QSlider class, In the above code, we have created an instance of QSlider and associated it with our main window. after that we set the range of selectable values to be between 0 and 100, with the initial value set to 50. we also set the tick interval to 10 and position the ticks below the slider.
1 2 3 4 5 |
slider = QSlider(Qt.Orientation.Horizontal, window) slider.setRange(0, 100) slider.setValue(50) slider.setTickInterval(10) slider.setTickPosition(QSlider.TickPosition.TicksBelow) |
For handling value changes in the slider, we can connect the valueChanged signal to a custom function, In this example handle_value_change function will be called whenever the value in the slider changes, and it will change the text of the label.
1 2 3 4 |
def handle_value_change(value): label.setText(f"Selected value: {value}") slider.valueChanged.connect(handle_value_change) |
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 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
from PyQt6.QtWidgets import QApplication, QMainWindow, QSlider, QLabel, QVBoxLayout, QWidget from PyQt6.QtCore import Qt import sys def create_slider(): app = QApplication(sys.argv) window = QMainWindow() window.setWindowTitle("Slider Example") # Create the Slider slider = QSlider(Qt.Orientation.Horizontal, window) slider.setRange(0, 100) slider.setValue(50) slider.setTickInterval(10) slider.setTickPosition(QSlider.TickPosition.TicksBelow) # Create the Label label = QLabel("Selected value: ") # Create a layout and add the Slider and Label to it layout = QVBoxLayout() layout.addWidget(slider) layout.addWidget(label) # Handle value changes in the Slider def handle_value_change(value): label.setText(f"Selected value: {value}") slider.valueChanged.connect(handle_value_change) # Create a widget to hold the layout widget = QWidget() widget.setLayout(layout) # Set the central widget of the main window window.setCentralWidget(widget) window.show() sys.exit(app.exec()) if __name__ == '__main__': create_slider() |
Run your code and this will be the result

Learn More on PyQt6
- How to Create GUI Button in PyQt6
- How to Add Image in PyQt6 Applications
- How to Add CSS Stylesheet in PyQt6
- How to Add Animation and Effects in PyQt6
- How to Create TextBox in PyQt6
- How to Create SpinBox in PyQt6