This article mainly introduces the slider control that you must learn every day in PyQt5. It has a certain reference value. Interested friends can refer to it
QSlider is a handle that can be pulled back and forth. of controls. Sometimes using a slider is more convenient than entering numbers or using a spin box.
In our example we will create a slider and a label. Label display image. The slider will control the image displayed by the label.
#!/usr/bin/python3 # -*- coding: utf-8 -*- """ PyQt5 教程 这个例子显示了一个QSlider控件的使用方法。 作者:我的世界你曾经来过 博客:http://blog.csdn.net/weiaitaowang 最后编辑:2016年8月3日 """ import sys from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QSlider from PyQt5.QtGui import QPixmap from PyQt5.QtCore import Qt class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): sld = QSlider(Qt.Horizontal, self) sld.setFocusPolicy(Qt.NoFocus) sld.setGeometry(30, 40, 100, 30) sld.valueChanged[int].connect(self.changeValue) self.label = QLabel(self) self.label.setPixmap(QPixmap('F:\Python\PyQt5\Widgets\images\mute.png')) self.label.setGeometry(160, 30, 80, 50) self.setGeometry(300, 300, 280, 170) self.setWindowTitle('滑块控件') self.show() def changeValue(self, value): if value == 0: self.label.setPixmap(QPixmap('F:\Python\PyQt5\Widgets\images\mute.png')) elif value > 0 and value <= 30: self.label.setPixmap(QPixmap('F:\Python\PyQt5\Widgets\images\min.png')) elif value > 30 and value < 80: self.label.setPixmap(QPixmap('F:\Python\PyQt5\Widgets\images\med.png')) else: self.label.setPixmap(QPixmap('F:\Python\PyQt5\Widgets\images\max.png')) if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())
In our example, we simulate a volume control. By dragging the slider's handle, we change the image on the label.
sld = QSlider(Qt.Horizontal, self)
Create a horizontal slider QSlider
self.label = QLabel(self) self.label.setPixmap(QPixmap('F:\Python\PyQt5\Widgets\images\mute.png'))
Create a label QLabel control and Set the initial image to be displayed
sld.valueChanged[int].connect(self.changeValue)
Connect the slider's valueChanged signal to the changeValue() method (slot)
if value == 0: self.label.setPixmap(QPixmap('F:\Python\PyQt5\Widgets\images\mute.png'))
We set the image on the label based on the value of the slider. In the above code, if the slider equals zero the label's image is set to mute.png.
After the program is executed
Related recommendations:
##PyQt5 must learn the switch button every day_python
PyQT realizes multi-window switching
PyQt5 events and signals that must be learned every day
The above is the detailed content of PyQt5 must learn the slider control QSlider_python every day. For more information, please follow other related articles on the PHP Chinese website!