3

Всем привет!
Подскажите, как получить цвет рамки окна, который автоматически изменяется при смене темы?

Вроде нашел функцию на C++ : GetSysColor(COLOR_WINDOWFRAME) (описание функции на microsoft.com) , но не получается скрестить с Python 3.6 и PyQt5

Хочу сделать динамический фон кнопок с градиентом на основе цвета рамки в моем небольшом приложении:

введите сюда описание изображения

Ссылка на приложение (github)

2 ответа 2

2

Мне подумалось, а что если сделать скриншот окна и вытащить цвет из заголовка.


from PyQt5 import Qt


if __name__ == '__main__':
    app = Qt.QApplication([])

    # NOTE: для отладки
    # label = Qt.QLabel()

    def get_color():
        rect = button.rect()
        indent = 5

        pixmap = app.primaryScreen().grabWindow(button.winId(), rect.x(), rect.y() - indent, indent, indent)
        # label.setPixmap(pixmap)
        # label.setScaledContents(True)
        # label.resize(400, 400)
        # label.show()

        color = pixmap.toImage().pixelColor(0, 0)
        button.setStyleSheet('background-color: ' + color.name())

    button = Qt.QPushButton('Get')
    button.clicked.connect(get_color)
    button.resize(100, 100)
    button.show()

    app.exec()
1
  • Гениально! Сработало, большое спасибо!
    – samwl
    26 апр 2018 в 12:23
2

Ссылка на ответ:

QColor getWindowFrameColor() {
    // This is the only way to detect that a given color is supported
    HBRUSH brush = GetSysColorBrush(COLOR_ACTIVEBORDER);
    if (brush) {
        DWORD color = GetSysColor(COLOR_ACTIVEBORDER);
        return QColor(GetRValue(color), GetGValue(color), GetBValue(color));
        // calling DeleteObject(brush) is unnecessary, but would be harmless
    }
    return QColor();
}

Переписал функцию для PyQt5:

from PyQt5 import Qt

COLOR_ACTIVEBORDER = 10

import ctypes
GetSysColorBrush = ctypes.windll.user32.GetSysColorBrush
GetSysColor = ctypes.windll.user32.GetSysColor


def GetRValue(value):
    return value & 0xff


def GetGValue(value):
    return value >> 8 & 0xff


def GetBValue(value):
    return value >> 16 & 0xff


def getWindowFrameColor():
    # This is the only way to detect that a given color is supported
    brush = GetSysColorBrush(COLOR_ACTIVEBORDER)
    if brush:
        color = GetSysColor(COLOR_ACTIVEBORDER)
        return Qt.QColor(GetRValue(color), GetGValue(color), GetBValue(color))

    return Qt.QColor()


if __name__ == '__main__':
    app = Qt.QApplication([])

    def get_color():
        color = getWindowFrameColor()
        button.setStyleSheet('background-color: ' + color.name())

    button = Qt.QPushButton('Get')
    button.clicked.connect(get_color)
    button.resize(100, 100)
    button.show()

    app.exec()

Результат:

введите сюда описание изображения

введите сюда описание изображения

11
  • К сожалению, возвращает только серый цвет (180, 180, 180)
    – samwl
    24 апр 2018 в 7:06
  • Меняли цвет рамки? Я на скринах показывал пример
    – gil9red
    24 апр 2018 в 7:13
  • Да, но в любом случае возвращает серый
    – samwl
    24 апр 2018 в 7:17
  • yadi.sk/i/WrMd5Zhw3Uhza6
    – samwl
    24 апр 2018 в 7:18
  • возможно ли узнать цвет без использования PyQt?
    – samwl
    24 апр 2018 в 7:24

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.