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

2022-02-24 00:00:00 python pyqt progress-bar slider

问题描述

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

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

这是输出组件

相关文章