【问题标题】:How to get groups and roles of a QPalette in PyQt/PySide?如何在 PyQt/PySide 中获取 QPalette 的组和角色?
【发布时间】:2015-03-11 08:32:08
【问题描述】:

我如何在 PyQt/PySide 应用程序中查询这些值,而不是“手动”定义列表 groupsroles(在下面的代码中)?

from PyQt4 import QtGui

groups = ['Disabled', 'Active', 'Inactive', 'Normal']
roles = [
            'AlternateBase',
            'Background', 
            'Base',
            'Button',
            'ButtonText',
            'BrightText',
            'Dark',
            'Foreground',
            'Highlight',
            'HighlightedText',
            'Light',
            'Link',
            'LinkVisited',
            'Mid',
            'Midlight',
            'Shadow',
            'ToolTipBase',
            'ToolTipText',
            'Text',
            'Window',
            'WindowText'
        ]

def getPaletteInfo():
    palette = QtGui.QApplication.palette()
    #build a dict with all the colors
    result = {}    
    for role in roles:
        print role
        for group in groups:
            qGrp = getattr(QtGui.QPalette, group)
            qRl = getattr(QtGui.QPalette, role)
            result['%s:%s' % (role, group)] =  palette.color(qGrp, qRl).rgba()
    return result

【问题讨论】:

    标签: pyqt pyside qpalette


    【解决方案1】:

    这可以通过标准的 Python 内省技术来完成:

    for name in dir(QtGui.QPalette):
        if isinstance(getattr(QtGui.QPalette, name), QtGui.QPalette.ColorGroup):
            print(name)
    

    QtGui.QPalette.ColorRole 也可以做到这一点。

    但请注意,这会产生一些您可能没有预料到的额外项目。有NColorGroupsNColorRoles。给出每个枚举中的项目数;有几个同义词,如Window/Background;以及其他一两个,例如AllNoRole

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-24
      • 2015-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-22
      • 2013-04-29
      相关资源
      最近更新 更多