This article covers the QSlider widget in Python PyQt.
The PyQt QSlider widget provides the user with a GUI friendly way of picking a value from a range of different values. The Slider widget has a handle that you can move across a groove. As you move the slider, the selected value changes accordingly.
You will find a complete list of methods and options for the PyQt Slider available at the end of this article.
Creating a Slider
To create a basic Slider you will need the QtWidgets.QSlider()
function. By default, the orientation is vertical. For a horizontal slider, you need to use Qt.Horizontal
.
Next you will need to set a minimum and maximum range for it, with the help of the setMinimum()
and setMaximum()
methods.
Using the setGeometry()
method we decide both the location and the dimensions of the Slider. The first two parameters represent the X and Y position of the Slider on the window. The third and fourth parameters are the width and height of the Slider.
Finally you have to set the Ticks. The Ticks are those little markers on the side of the Slider. You can pick one out of many different Tick placements (see the end of the article) and set an interval for the distance between the Ticks.
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import Qt
import sys
app = QApplication(sys.argv)
win = QMainWindow()
win.setGeometry(400,400,300,300)
win.setWindowTitle("CodersLegacy")
slider = QtWidgets.QSlider(Qt.Horizontal, win)
slider.setGeometry(50,50, 200, 50)
slider.setMinimum(0)
slider.setMaximum(20)
slider.setTickPosition(QtWidgets.QSlider.TicksBelow)
slider.setTickInterval(2)
win.show()
sys.exit(app.exec_())
The GUI output of the above code.
Retrieving Slider Values
We’ve added another widget called QLabel here to help us display the values from the QSlider widget. Be sure to convert any integer values to strings before trying to display them as Labels only shown string values.
Using the valueChanged()
signal, we can link the slider’s value to the display()
function. Any time the value of the slider is changed, the display function will be called, and the value updated.
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import Qt
import sys
def display():
my_label.setText(str(slider.value()))
app = QApplication(sys.argv)
win = QMainWindow()
win.setGeometry(400,400,300,300)
win.setWindowTitle("CodersLegacy")
slider = QtWidgets.QSlider(Qt.Horizontal, win)
slider.setGeometry(50,50, 200, 50)
slider.setMinimum(0)
slider.setMaximum(20)
slider.setTickPosition(QtWidgets.QSlider.TicksBelow)
slider.setTickInterval(2)
slider.valueChanged.connect(display)
my_label = QtWidgets.QLabel(win)
my_label.move(100, 100)
win.show()
sys.exit(app.exec_())
You can also link the Slider to the QPushButton widget in order to retrieve values. However, with this approach, the value will only be returned when you press the button.
QSlider Methods
This is a compilation of the most common and important methods for the QSlider widget.
Method | Description |
---|---|
setMaximum() | Defines the minimum value for the Slider. |
setMinimum() | Defines the maximum value for the Slider. |
setSingleStep() | Sets the increment between values. |
setValue() | Sets the current value of the Slider. |
value() | Returns the current value of the Slider. |
setTickInterval() | Sets the distance between ticks on the Slider. |
setTickPosition() | Possible options: 1. QSlider.NoTicks 2. QSlider.TicksBothSides 3. QSlider.TicksAbove 4. QSlider.TicksBelow 5. QSlider.TicksLeft 6. QSlider.TicksRight |
This marks the end of the Python PyQt QSlider article. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the article content can be asked in the comments section below.
Head over to the main PyQt5 section to learn about other amazing widgets.