在 PyQt5 中更改小部件的高亮颜色 [英] Changing widget's highlight color in PyQt5

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

问题描述

我已经知道如何使用 setStyleSheet() 更改 PyQt5 小部件的背景颜色,但我找不到如何更改它们的高亮颜色.我 100% 确定此选项应该在 tkinter 中可用.

I've figured out how to change the background color of PyQt5 widgets using setStyleSheet() but I cannot find how to change their highlight color. I am 100% sure this option should be available as in tkinter.

from PyQt5.QtWidgets import QWidget, QApplication, QPushButton
import sys

class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.create_widgets()

    def create_widgets(self):
        b1 = QPushButton(parent=self, text='Button')
        color_1 = 'red'
        color_2 = 'blue'
        b1.setStyleSheet('QWidget {background-color: %s}' % color_1)
        #b1.setStyleSheet('QWidget {highlight-color: %s}' % color_2) does not work
        b1.resize(150,50)
        b1.move(100,100)

        self.setGeometry(300,200, 400, 400)
        self.show()

if __name__ == '__main__':
    app = QApplication([])
    ex = Example()
    sys.exit(app.exec_())

非常感谢任何帮助!

推荐答案

要自定义 QPushButton,您可以使用以下样式表:

To customize QPushButton you can use the following style sheet:

import sys
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton

class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.create_widgets()

    def create_widgets(self):
        b1 = QPushButton(parent=self, text='Button')
        b1.setGeometry(150,150, 100, 100)


style = '''
QWidget {
    background-color: coral; 
} 

QPushButton {
    background-color: #006325;
    font-size: 20px;
    color: white;

    min-width:  100px;
    max-width:  100px;
    min-height: 100px;
    max-height: 100px;

    border-radius: 50px;        
    border-width: 1px;
    border-color: #ae32a0;
    border-style: solid;
}
QPushButton:hover {
    background-color: #328930;
    color: yellow;
}
QPushButton:pressed {
    background-color: #80c342;
    color: red;
}    

'''


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

    app.setStyleSheet(style)             # <---

    ex = Example()
    ex.setGeometry(300,200, 400, 400)
    ex.setWindowTitle("QPushButton - style sheet")
    ex.show()
    sys.exit(app.exec_())

这篇关于在 PyQt5 中更改小部件的高亮颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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