如何在与pyqt4一起使用的滑块中显示范围值

时间:2018-11-29 05:13:38

标签: python pyqt pyqt4 qslider

这是我的代码,我想显示滑块的最小和最大范围值。我尝试了很多方法,但是我什么也没得到。有人可以帮助我如何显示滑块,如下面的图像所示。

下面是我的代码:

from pyface.qt import QtGui, QtCore
import sys

class Example(QtGui.QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):
        self.vbox = QtGui.QVBoxLayout()
        self.label3 = QtGui.QLabel()
        self.slider = QtGui.QSlider()
        self.slider.setMinimum(0)
        self.slider.setMaximum(100)
        self.slider.setTickPosition(QtGui.QSlider.TicksLeft)
        self.slider.setOrientation(QtCore.Qt.Horizontal)
        self.slider.setOrientation(QtCore.Qt.Horizontal)
        self.vbox.addWidget(self.slider,QtCore.Qt.AlignBottom)
        self.vbox.addWidget(self.label3)
        self.setLayout(self.vbox)
        self.setGeometry(300, 300, 300, 150)
        self.slider.valueChanged.connect(self.valuechange)
        self.show()

    def valuechange(self):
        txt = str(self.slider.value())
        self.label3.setText(txt)

def main():    
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以将2个QLabel与QHBoxLayout和QVBoxLayout结合使用:

from pyface.qt import QtGui, QtCore
import sys

class Slider(QtGui.QSlider):
    minimumChanged = QtCore.Signal(int)
    maximumChanged = QtCore.Signal(int)

    def setMinimum(self, minimum):
        self.minimumChanged.emit(minimum)
        super(Slider, self).setMinimum(minimum)

    def setMaximum(self, maximum):
        self.maximumChanged.emit(maximum)
        super(Slider, self).setMaximum(maximum)

class Example(QtGui.QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):
        self.label = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter)

        self.slider = Slider(tickPosition=QtGui.QSlider.TicksLeft,
            orientation=QtCore.Qt.Horizontal)
        slider_vbox = QtGui.QVBoxLayout()
        slider_hbox = QtGui.QHBoxLayout()
        slider_hbox.setContentsMargins(0, 0, 0, 0)
        slider_vbox.setContentsMargins(0, 0, 0, 0)
        slider_vbox.setSpacing(0)
        label_minimum = QtGui.QLabel(alignment=QtCore.Qt.AlignLeft)
        self.slider.minimumChanged.connect(label_minimum.setNum)
        label_maximum = QtGui.QLabel(alignment=QtCore.Qt.AlignRight)
        self.slider.maximumChanged.connect(label_maximum.setNum)
        slider_vbox.addWidget(self.slider)
        slider_vbox.addLayout(slider_hbox)
        slider_hbox.addWidget(label_minimum, QtCore.Qt.AlignLeft)
        slider_hbox.addWidget(label_maximum, QtCore.Qt.AlignRight)
        slider_vbox.addStretch()

        self.slider.setMinimum(0)
        self.slider.setMaximum(100)

        vbox = QtGui.QVBoxLayout(self)
        vbox.addLayout(slider_vbox)
        vbox.addWidget(self.label)
        self.setGeometry(300, 300, 300, 150)
        self.slider.valueChanged.connect(self.label.setNum)
        self.show()

def main():    
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

enter image description here