改变 QProgressbar() 的颜色

发布于 2024-11-14 13:47:31 字数 490 浏览 4 评论 0原文

我想知道是否可以改变 PyQt 进度条的颜色?

我有以下代码:

from PyQt4 import QtGui, QtCore
Pbar1 = QtGui.QProgressBar()
Pbar1.setParent(Frame1)
Pbar1.setGeometry(0, 0, 306, 30)
Pbar1.setValue(Frame1Value)
if Pbar1.value == 100
......Pbar1.setColor(Red)

Frame1Value 依赖于一些早期计算,并且假设永远不会计算出完全相同的值。

我希望进度条在值为 100 时变为“红色”(该值被设置为有限,因为进度条不会显示高于 100% 的值),这样可以让我更好地直观地了解该值是“不受约束”。

我知道 .setColor 不是进度条的已知命令,但这只是为了展示我的想法。

有谁知道如何做到这一点,或者是否可能?

提前谢谢!

I was wondering whether it's possible to change the color of a PyQt Progressbar?

I have the following code:

from PyQt4 import QtGui, QtCore
Pbar1 = QtGui.QProgressBar()
Pbar1.setParent(Frame1)
Pbar1.setGeometry(0, 0, 306, 30)
Pbar1.setValue(Frame1Value)
if Pbar1.value == 100
......Pbar1.setColor(Red)

Frame1Value is dependable on some early calculations, and to be assumed never calculates to exactly the same value.

I would like the progressbar to turn 'red' when the value is 100 (which the value is set limited at since a progressbar won't show values above 100%), so giving me a better visual image of the fact that the value is 'out of constraint'.

I am aware that .setColor isn't a known command for a Progressbar, but it's just to show my idea.

Does anyone know how to do this, or if it is even possible??

Thx in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

π浅易 2024-11-21 13:47:31

您可以子类化 QProgressBar 并使用一些样式表
请参阅使用样式表自定义 Qt 小部件自定义 QProgressBar:

from PyQt4 import QtGui, QtCore

DEFAULT_STYLE = """
QProgressBar{
    border: 2px solid grey;
    border-radius: 5px;
    text-align: center
}

QProgressBar::chunk {
    background-color: lightblue;
    width: 10px;
    margin: 1px;
}
"""

COMPLETED_STYLE = """
QProgressBar{
    border: 2px solid grey;
    border-radius: 5px;
    text-align: center
}

QProgressBar::chunk {
    background-color: red;
    width: 10px;
    margin: 1px;
}
"""

class MyProgressBar(QtGui.QProgressBar):
    def __init__(self, parent = None):
        QtGui.QProgressBar.__init__(self, parent)
        self.setStyleSheet(DEFAULT_STYLE)

    def setValue(self, value):
        QtGui.QProgressBar.setValue(self, value)

        if value == self.maximum():
            self.setStyleSheet(COMPLETED_STYLE)

unfinished completed

另一种解决方案是将调色板重新分配给 QProgressBar,这将允许您拥有“样式感知”组件:

class MyProgressBar(QtGui.QProgressBar):
    def setValue(self, value):
        QtGui.QProgressBar.setValue(self, value)
        if value == self.maximum():
            palette = QtGui.QPalette(self.palette())
            palette.setColor(QtGui.QPalette.Highlight, 
                             QtGui.QColor(QtCore.Qt.red))
            self.setPalette(palette)

You can sublass QProgressBar and use some style sheet
see Customizing Qt Widgets Using Style Sheets and Customizing QProgressBar:

from PyQt4 import QtGui, QtCore

DEFAULT_STYLE = """
QProgressBar{
    border: 2px solid grey;
    border-radius: 5px;
    text-align: center
}

QProgressBar::chunk {
    background-color: lightblue;
    width: 10px;
    margin: 1px;
}
"""

COMPLETED_STYLE = """
QProgressBar{
    border: 2px solid grey;
    border-radius: 5px;
    text-align: center
}

QProgressBar::chunk {
    background-color: red;
    width: 10px;
    margin: 1px;
}
"""

class MyProgressBar(QtGui.QProgressBar):
    def __init__(self, parent = None):
        QtGui.QProgressBar.__init__(self, parent)
        self.setStyleSheet(DEFAULT_STYLE)

    def setValue(self, value):
        QtGui.QProgressBar.setValue(self, value)

        if value == self.maximum():
            self.setStyleSheet(COMPLETED_STYLE)

unfinished completed

Another solution would be to reassign a palette to the QProgressBar which will allow you to have a "style aware" component:

class MyProgressBar(QtGui.QProgressBar):
    def setValue(self, value):
        QtGui.QProgressBar.setValue(self, value)
        if value == self.maximum():
            palette = QtGui.QPalette(self.palette())
            palette.setColor(QtGui.QPalette.Highlight, 
                             QtGui.QColor(QtCore.Qt.red))
            self.setPalette(palette)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文