繁体   English   中英

在PyQt5中更改小部件的突出显示颜色

[英]Changing widget's highlight color in PyQt5

我已经弄清楚了如何使用setStyleSheet()更改PyQt5小部件的背景颜色,但是我找不到如何更改其突出显示颜色。 我100%肯定应该在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,可以使用以下样式表:

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_())

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM