QSlider is a control with a handle that can be pulled back and forth. Sometimes it is more convenient to use a slider than to enter numbers or use a spinning box.
In our example, we will create a slider and a label. The label displays the image. The slider will control the image displayed by the label.
#!/usr/bin/python3 # -*- coding: utf-8 -*- """ PyQt5 Tutorial This example shows the use of a QSlider control. Author: My World You've Been Here Before Blog: /weiaitaowang Last Edit: August 3, 2016 """ import sys from import QApplication, QWidget, QLabel, QSlider from import QPixmap from import Qt class Example(QWidget): def __init__(self): super().__init__() () def initUI(self): sld = QSlider(, self) () (30, 40, 100, 30) [int].connect() = QLabel(self) (QPixmap('F:\Python\PyQt5\Widgets\images\')) (160, 30, 80, 50) (300, 300, 280, 170) ('Slider control') () def changeValue(self, value): if value == 0: (QPixmap('F:\Python\PyQt5\Widgets\images\')) elif value > 0 and value <= 30: (QPixmap('F:\Python\PyQt5\Widgets\images\')) elif value > 30 and value < 80: (QPixmap('F:\Python\PyQt5\Widgets\images\')) else: (QPixmap('F:\Python\PyQt5\Widgets\images\')) if __name__ == '__main__': app = QApplication() ex = Example() (app.exec_())
In our example, we simulate a volume control. By dragging the handle of the slider, we change the image on the label.
sld = QSlider(, self)
Create a horizontal slider QSlider
= QLabel(self) (QPixmap('F:\Python\PyQt5\Widgets\images\'))
Create a label QLabel control and set the initial image to be displayed.
[int].connect()
Connect the valueChanged signal of the slider to the changeValue() method (slot)
if value == 0: (QPixmap('F:\Python\PyQt5\Widgets\images\'))
We set the image on the label based on the value of the slider. In the above code, if the slider is equal to zero the image of the label is set.
post-program execution
This is the whole content of this article, I hope it will help you to learn more.