如何在Python中创建可以用鼠标修改的图形滑块? [英] How to create graphic slider in Python that can be modified with mouse?

查看:43
本文介绍了如何在Python中创建可以用鼠标修改的图形滑块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在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()

这是输出组件

这篇关于如何在Python中创建可以用鼠标修改的图形滑块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆