This article mainly for you in detail the PYQT5 every day must learn slider control, with a certain reference value, interested in small partners can refer to
Qslider is a control that has a handle that can be pulled back and forth. Sometimes it is easier to use a slider than to enter a number or use a spin box.
In our example, we will create a slider and a label. The label displays the image. The slider controls the image that the label displays.
#!/usr/bin/python3#-*-coding:utf-8-*-"" "PYQT5 Tutorial This example shows how a Qslider control is used. Author: My World You've been to the blog: Http://blog.csdn.net/weiaitaowang Last edited: August 3, 2016 "" "Import sysfrom pyqt5.qtwidgets Import Qapplication, Qwidget, Qlabel, qsliderfrom Pyqt5.qtgui import qpixmapfrom pyqt5.qtcore import Qtclass Example (QWidget): D EF __init__ (self): Super (). __init__ () Self.initui () def initui (self): SLD = Qslider (Qt.horizontal, self) sld.setfocusp Olicy (Qt.nofocus) sld.setgeometry (() 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 , Self.setgeometry, 280, self.setwindowtitle (' Slider control ') 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.setPixma P (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 the volume control. By dragging the handle of the slider, 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 display
Sld.valuechanged[int].connect (Self.changevalue)
Connect the valuechanged signal of the slider 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 the zero label, the image is set to Mute.png.
After the program executes