简体   繁体   English

如何仅编辑 PyQT5 小部件的背景颜色

[英]How to edit only background-color for PyQT5 widgets

I have a problem with styles in PyQT5. I would like to modify something in the "Fusion" style: when the page loses focus, the blue of some widgets becomes white, i would like to keep them blue.我在 PyQT5 中遇到了 styles 的问题。我想修改“Fusion”样式中的一些内容:当页面失去焦点时,某些小部件的蓝色变为白色,我希望它们保持蓝色。

But when i try to edit only the background color for a QprogressBar, the text is no more centered and there are some other changes.但是当我尝试只编辑 QprogressBar 的背景颜色时,文本不再居中并且还有一些其他变化。 ( app.setStyleSheet("QProgressBar::chunk { background-color: blue}") ) ( app.setStyleSheet("QProgressBar::chunk { background-color: blue}") )

I also tried app.my_progress_bar.setStyleSheed("background-color: blue") which seems to keep text centered but i don't know how to do it for "chunk" item.我还尝试app.my_progress_bar.setStyleSheed("background-color: blue")这似乎使文本居中但我不知道如何为“块”项目做这件事。

Here is a little script if you want to test a solution:如果你想测试一个解决方案,这里有一个小脚本:

import sys
import time
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtWidgets import QWidget, QPushButton, QProgressBar, QVBoxLayout, QApplication

class Thread(QThread):
    _signal = pyqtSignal(int)
    def __init__(self):
        super(Thread, self).__init__()

    def __del__(self):
        self.wait()

    def run(self):
        for i in range(100):
            time.sleep(0.1)
            self._signal.emit(i)

class Example(QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.setWindowTitle('QProgressBar')
        self.btn = QPushButton('Click me')
        self.btn.clicked.connect(self.btnFunc)
        self.pbar = QProgressBar(self)
        self.pbar.setValue(0)
        self.resize(300, 100)
        self.vbox = QVBoxLayout()
        self.vbox.addWidget(self.pbar)
        self.vbox.addWidget(self.btn)
        self.setLayout(self.vbox)
        self.show()

    def btnFunc(self):
        self.thread = Thread()
        self.thread._signal.connect(self.signal_accept)
        self.thread.start()
        self.btn.setEnabled(False)

    def signal_accept(self, msg):
        self.pbar.setValue(int(msg))
        if self.pbar.value() == 99:
            self.pbar.setValue(0)
            self.btn.setEnabled(True)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    app.setStyle("Fusion") ##### When the main windows loses focus, the progressbar becomes white instead of blue
    ex = Example()
    ex.show()
    sys.exit(app.exec_())

When the window have the focus:当 window 有焦点时:
在此处输入图像描述

When the window does not have the focus:当window没有焦点时:
在此处输入图像描述

There is no need to use style sheets as long as the color roles of the widget are known.只要小部件的颜色角色已知,就不需要使用样式表。

Specifically, QProgressBar normally uses the Highlight role, which has a different color for the Inactive color group , so you just need to override it.具体来说,QProgressBar 通常使用Highlight作用,它对Inactive color group有不同的颜色,所以你只需要覆盖它。

        palette = self.pbar.palette()
        palette.setBrush(
            palette.Inactive, palette.Highlight, palette.highlight())
        self.pbar.setPalette(palette)

Note that the palette is only a reference, it's completely up to the style to decide which group/role use for a widget (or even completely ignore it).请注意,调色板只是一个参考,它完全取决于样式来决定哪个组/角色用于一个小部件(或者甚至完全忽略它)。 If you use another style than Fusion, the above might not work as expected.如果您使用 Fusion 以外的其他样式,则上述内容可能无法按预期工作。

use stylesheet for QProgressBar to achieve centered text and QProgressBar::chunk to keep background color even if it loses focus使用 QProgressBar 的样式表实现居中文本和 QProgressBar::chunk 以保持背景颜色,即使它失去焦点

import sys
import time
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtWidgets import QWidget, QPushButton, QProgressBar, QVBoxLayout, QApplication

class Thread(QThread):
    _signal = pyqtSignal(int)
    def __init__(self):
        super(Thread, self).__init__()

    def __del__(self):
        self.wait()

    def run(self):
        for i in range(100):
            time.sleep(0.1)
            self._signal.emit(i)

class Example(QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.setWindowTitle('QProgressBar')
        self.btn = QPushButton('Click me')
        self.btn.clicked.connect(self.btnFunc)
        self.pbar = QProgressBar(self)
        self.pbar.setValue(0)
        # setting background color
        self.pbar.setStyleSheet(
                         "QProgressBar"
                          "{"
                          "text-align:center;"
                          "}"
                          "QProgressBar::chunk"
                          "{"
                          "background-color : blue;"
                          "text-align:center;"
                          "color : #E0E0E0;"
                          "border : 1px"
                          "}"
                         )
        self.resize(300, 100)
        self.vbox = QVBoxLayout()
        self.vbox.addWidget(self.pbar)
        self.vbox.addWidget(self.btn)
        self.setLayout(self.vbox)
        self.show()

    def btnFunc(self):
        self.thread = Thread()
        self.thread._signal.connect(self.signal_accept)
        self.thread.start()
        self.btn.setEnabled(False)

    def signal_accept(self, msg):
        self.pbar.setValue(int(msg))
        if self.pbar.value() == 99:
            self.pbar.setValue(0)
            self.btn.setEnabled(True)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    app.setStyle("Fusion") ##### When the main windows loses focus, the progressbar becomes white instead of blue
    ex = Example()
    ex.show()
    sys.exit(app.exec_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM