如何在PyQt5中更改QButtonGroup的颜色属性? [英] how to change the color properties of QButtonGroup in PyQt5?

查看:60
本文介绍了如何在PyQt5中更改QButtonGroup的颜色属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在PyQt5中更改按钮组对象的按钮颜色.我尝试过

I want to change the color of the button of a button group object in PyQt5. I tried

QButtonGroup.setStyleSheet("""
                              QButtonGroup 
                                { 
                                        background-color: rgb(255, 255,255);  
                                }
                                """
                                )

但是没有这样的功能.如果有人可以(在Python或C ++中)提供帮助,我将不胜感激

But there is no such a function. I would appreciate if anyone can help (either in Python or in C++ )

推荐答案

根据 Qt文档:

QButtonGroup提供了一个抽象容器,按钮小部件已放入其中可以放置.它不提供对此的视觉表示容器(有关容器小部件,请参见QGroupBox),而是进行管理组中每个按钮的状态.

QButtonGroup provides an abstract container into which button widgets can be placed. It does not provide a visual representation of this container (see QGroupBox for a container widget), but instead manages the states of each of the buttons in the group.

因此,您不能为其设置样式表.也许您想要QGroupBox?这是一个示例:

Therefore, you cannot set a stylesheet to it. Maybe you want a QGroupBox? Here is an example:

import sys
import PyQt5.QtWidgets as QtWidgets

def window():
    app = QtWidgets.QApplication(sys.argv)
    w = QtWidgets.QWidget()
    w.setWindowTitle('Hello')
    w.setGeometry(100,100,200,100)
    g = QtWidgets.QGroupBox(w)
    layout = QtWidgets.QVBoxLayout()
    b = QtWidgets.QPushButton(w)
    b.setText("Hello World!")
    b1 = QtWidgets.QPushButton(w)
    b1.setText("Hello SO!")
    layout.addWidget(b)
    layout.addWidget(b1)
    g.setLayout(layout)
    w.setStyleSheet("""
        QGroupBox { background-color: rgb(255, 255,255);
        } """)
    w.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    window()

这篇关于如何在PyQt5中更改QButtonGroup的颜色属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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