4

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

下面是我的代码:

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

在此处输入图像描述

4

1 回答 1

4

您可以将 2 个 QLabels 与 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()

在此处输入图像描述

于 2018-11-29T06:03:32.590 回答