Python QT 更改按钮的背景颜色,而不仅仅是边框 [英] Python QT change the background color of a button, not just border

查看:176
本文介绍了Python QT 更改按钮的背景颜色,而不仅仅是边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 python Qt 更改边框的背景颜色,但只设置了边框.

I am trying to change the background color of a border using python Qt but only the border gets set.

这是代码:

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import * 
from PyQt5.QtCore import pyqtSlot
import math
import sys

class Frame(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):      

        self.setGeometry(300, 100, 1000, 500)

        pal = QPalette()
        self.setAutoFillBackground(True)
        self.setPalette(pal)

        button = QPushButton('Clear Knots')
        button.clicked.connect(quit)
        pal.setColor(QPalette.Button, QColor(255,0,0,255))
        button.setPalette(pal)
        button.setAutoFillBackground(True)
        #button.setStyleSheet("background-color: red;")

        Layout = QGridLayout()
        Layout.addWidget(button, 2,0)
        self.setLayout(Layout)

        self.show()



if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Frame()
    sys.exit(app.exec_())

结果:

推荐答案

试试看:

import sys
from PyQt5.QtWidgets import QWidget, QHBoxLayout, QPushButton, QApplication
from PyQt5.QtGui     import QIcon
from PyQt5.QtCore    import pyqtSlot, QSize


class Window(QWidget):

    def __init__(self, *args, **kwargs):
        super(Window, self).__init__(*args, **kwargs)

        self.setGeometry(800, 65, 500, 200)

        layout = QHBoxLayout(self)
        layout.addWidget(QPushButton("red button", self,
                                     objectName="RedButton", minimumHeight=48))
        # ----------------------------------------------------------------------
        button = QPushButton('  \n   PyQt5\n   button\n  ', self, 
                                     objectName="GreenButton", minimumHeight=48)
        button.setIcon(QIcon("E:/_Qt/img/qt-logo.png"))
        button.setIconSize(QSize(48, 48))                                     
        layout.addWidget(button) 
        # ----------------------------------------------------------------------

        layout.addWidget(QPushButton("blue button", self,
                                     objectName="BlueButton", minimumHeight=48))
        layout.addWidget(QPushButton("orange button", self,
                                     objectName="OrangeButton", minimumHeight=48))
        layout.addWidget(QPushButton("purple button", self,
                                     objectName="PurpleButton", minimumHeight=48))


StyleSheet = '''
QPushButton {
    border: none;
}
QPushButton#RedButton {
    background-color: #f44336;
}
#RedButton:hover {
    background-color: #e57373; 
    color: #fff;
}
#RedButton:pressed { 
    background-color: #ffcdd2; 
}
#GreenButton {
    background-color: #4caf50;
    border-radius: 5px;       
}
#GreenButton:hover {
    background-color: #81c784;
    color: #fff;              
}
#GreenButton:pressed {
    background-color: #c8e6c9;
}
#BlueButton {
    background-color: #2196f3;
    min-width:  96px;
    max-width:  96px;
    min-height: 96px;
    max-height: 96px;
    border-radius: 48px;        
}
#BlueButton:hover {
    background-color: #64b5f6;
}
#BlueButton:pressed {
    background-color: #bbdefb;
}
#OrangeButton {
    max-height: 48px;
    border-top-right-radius:   20px;   
    border-bottom-left-radius: 20px;   
    background-color: #ff9800;
}
#OrangeButton:hover {
    background-color: #ffb74d;
}
#OrangeButton:pressed {
    background-color: #ffe0b2;
}

QPushButton[text="purple button"] {
    color: white;                    
    background-color: #9c27b0;
}
'''

if __name__ == "__main__":
    app = QApplication(sys.argv)
    app.setStyleSheet(StyleSheet)
    w = Window()
    w.setWindowTitle("Demo color-button")
    w.show()
    sys.exit(app.exec_())

这篇关于Python QT 更改按钮的背景颜色,而不仅仅是边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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