如何在滑块中显示范围值

how to display the range values in slider

这是我的代码,我想显示 slider.I 的最小和最大范围值尝试了很多方法但我没有得到 anything.Can 谁能帮助我如何将滑块显示为如下图所示。

下面是我的代码:

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()

您可以将 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()