如何在Python中创建可以用鼠标修改的图形滑块?

时间:2014-06-06 08:50:02

标签: python slider pyqt progress-bar

如何创建类似于PyQt中进度条的图形滑块,可以使用鼠标从0修改为100?

enter image description here

1 个答案:

答案 0 :(得分:3)

您必须正确设置QSlider样式表。该解决方案基于PyQt5

import sys
from PyQt5 import QtGui, QtCore, QtWidgets

class MyApp(object):    
    def __init__(self):
        super(MyApp, self).__init__()                
        self.mainWidget = QtWidgets.QWidget()
        self.mainLayout = QtWidgets.QVBoxLayout()
        self.mainWidget.setLayout(self.mainLayout)

        self.slider = QtWidgets.QSlider()
        self.slider.setOrientation(QtCore.Qt.Horizontal)
        self.slider.setStyleSheet(self.stylesheet())

        self.mainLayout.addWidget(self.slider)
        self.mainWidget.show()
        sys.exit(app.exec_())

    def stylesheet(self):
        return """
            QSlider::groove:horizontal {
                background: white;
                height: 40px;
            }

            QSlider::sub-page:horizontal {
                background: qlineargradient(x1: 0, y1: 0,    x2: 0, y2: 1,
                    stop: 0 #66e, stop: 1 #bbf);
                background: qlineargradient(x1: 0, y1: 0.2, x2: 1, y2: 1,
                    stop: 0 #bbf, stop: 1 #55f);
                height: 40px;
            }

            QSlider::add-page:horizontal {
                background: #fff;
                height: 40px;
            }

            QSlider::handle:horizontal {
                background: #bbf;
                border: 0px;
                width: 0px;
                margin-top: 0px;
                margin-bottom: 0px;
                border-radius: 0px;
            }
        """

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    MyApp()

这是输出组件

enter image description here