python - 在 PyQt5 中修改 Qgroupbox 的边框颜色而不修改其内部小部件的边框

标签 python colors pyqt

我正在尝试修改组框的颜色边框,但是当我这样做时,它也会修改内部小部件的边框,例如:

enter image description here

但我正在尝试得到类似的东西: enter image description here

这是我到目前为止的代码:

import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import * 

class tabdemo(QMainWindow):
    def __init__(self):
        super(tabdemo, self).__init__()
        self.setGeometry(50,50,500,500)
        self.centralWidget = QWidget()
        self.setCentralWidget(self.centralWidget)
        self.mainB  = QVBoxLayout()

        self.GB = QGroupBox("GroupBox")
        self.GB.setStyleSheet("QGroupBox { border: 1px solid red;}")
        self.GB.setFixedWidth(100)
        self.mainHBOX  = QVBoxLayout()

        self.GB1 = QGroupBox("GroupBox1")
        self.GB1.setFixedHeight(100)
        self.GB2 = QGroupBox("GroupBox2")
        self.GB2.setFixedHeight(100)
        self.GB3 = QGroupBox("GroupBox3")
        self.GB3.setFixedHeight(100)
        self.mainHBOX.addWidget(self.GB1)
        self.mainHBOX.addWidget(self.GB2)
        self.mainHBOX.addWidget(self.GB3)

        self.GB.setLayout(self.mainHBOX)

        self.mainB.addWidget(self.GB)

        self.centralWidget.setLayout(self.mainB)





def main():
   app = QApplication(sys.argv)
   ex = tabdemo()
   ex.show()
   sys.exit(app.exec_())

if __name__ == '__main__':
   main()

重要的一行是self.GB.setStyleSheet("QGroupBox { border: 1px Solid red;}")。它会更改边框颜色,但也会将颜色传播到子组框,但我不希望这样。

有人有解决办法吗?

最佳答案

您需要命名您的对象 (GroupBox) 并将样式表直接应用于该名称。将其添加到您的代码中:

        self.GB.setObjectName("ColoredGroupBox")  # Changed here...
        self.GB.setStyleSheet("QGroupBox#ColoredGroupBox { border: 1px solid red;}")  # ... and here

这是您修改后的代码:

import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

class tabdemo(QMainWindow):
    def __init__(self):
        super(tabdemo, self).__init__()
        self.setGeometry(50,50,500,500)
        self.centralWidget = QWidget()
        self.setCentralWidget(self.centralWidget)
        self.mainB  = QVBoxLayout()

        self.GB = QGroupBox("GroupBox")
        self.GB.setObjectName("ColoredGroupBox")  # Changed here...
        self.GB.setStyleSheet("QGroupBox#ColoredGroupBox { border: 1px solid red;}")  # ... and here
        self.GB.setFixedWidth(100)
        self.mainHBOX  = QVBoxLayout()

        self.GB1 = QGroupBox("GroupBox1")
        self.GB1.setFixedHeight(100)
        self.GB2 = QGroupBox("GroupBox2")
        self.GB2.setFixedHeight(100)
        self.GB3 = QGroupBox("GroupBox3")
        self.GB3.setFixedHeight(100)
        self.mainHBOX.addWidget(self.GB1)
        self.mainHBOX.addWidget(self.GB2)
        self.mainHBOX.addWidget(self.GB3)

        self.GB.setLayout(self.mainHBOX)

        self.mainB.addWidget(self.GB)

        self.centralWidget.setLayout(self.mainB)

def main():
   app = QApplication(sys.argv)
   ex = tabdemo()
   ex.show()
   sys.exit(app.exec_())

if __name__ == '__main__':
   main()

结果是这样的:

Name widget and add stylesheet to it

关于python - 在 PyQt5 中修改 Qgroupbox 的边框颜色而不修改其内部小部件的边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47415244/

相关文章:

python - 在 Bash/Python 中转换 CIM_DATETIME

python - 通过popen使用cdrecord不会弹出

更改c语言中的输入文本颜色

css - 如何在 div 悬停时更改跨度颜色

python - 单击菜单项时pyqt系统托盘图标退出

python - 向 PyQt5 QGroupBox 中的按钮添加功能

python - 在 PyQt 中创建带有行号的文本区域(textEdit)

python - 使用pymc用MCMC拟合两个正态分布(直方图)?

python - 如何更改 Tkinter ScrolledText 小部件的滚动条颜色?

python - 基于 Python/MySQL 的管道中的字符编码问题