pyqt5 中QSS

QSS(Qt Style Sheets)
Qt样式表

就是前端的CSS

用于设置控件的样式

from PyQt5.QtWidgets import *
import sys
class BasicQSS(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QSS样式")
        btn1 = QPushButton(self)
        btn1.setText("按钮1")
        btn2 = QPushButton(self)
        btn2.setText("按钮2")
        btn3 = QPushButton(self)
        btn3.setText("按钮3")
        vbox = QVBoxLayout()
        vbox.addWidget(btn1)
        vbox.addWidget(btn2)
        vbox.addWidget(btn3)
        self.setLayout(vbox)
if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = BasicQSS()
    # 选择器
    qssStyle = '''
        QPushButton {
            background-color:red
        }
    '''
    form.setStyleSheet(qssStyle)
    form.show()
    sys.exit(app.exec_())

pyqt5 中QSS_第1张图片
pyqt5 中QSS_第2张图片

选择器

from PyQt5.QtWidgets import *
import sys
import qdarkstyle
class QSSSelector(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QSS样式")
        btn1 = QPushButton(self)
        btn1.setText("按钮1")
        btn2 = QPushButton(self)
        btn2.setProperty('name','btn2')
        btn2.setText("按钮2")
        btn3 = QPushButton(self)
        btn3.setProperty('name','btn3')
        btn3.setText("按钮3")
        vbox = QVBoxLayout()
        vbox.addWidget(btn1)
        vbox.addWidget(btn2)
        vbox.addWidget(btn3)
        self.setLayout(vbox)
if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = QSSSelector()
    # 选择器
    qssStyle = '''
        QPushButton[name="btn2"] {
            background-color:red;
            color:yellow;
            height:120;
            font-size:60px;
        }
        QPushButton[name="btn3"] {
            background-color:blue;
            color:yellow;
            height:60;
            font-size:30px;
        }
    '''
    form.setStyleSheet(qssStyle)
    form.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
    form.show()
    sys.exit(app.exec_())

pyqt5 中QSS_第3张图片

QSS子控件选择器 QComboBox

from PyQt5.QtWidgets import *
import sys
class QSSSubControl(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QSS子控件选择器")
        combo = QComboBox(self)
        combo.setObjectName("myComboBox")
        combo.addItem("Window")
        combo.addItem("Linux")
        combo.addItem("Mac OS X")
        combo.move(50,50)
        self.setGeometry(250,200,320,150)
if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = QSSSubControl()
    qssStyle = '''
       QComboBox#myComboBox::drop-down {
           image:url(./images/dropdown.png)
       }
    '''
    form.setStyleSheet(qssStyle)
    form.show()
    sys.exit(app.exec_())

pyqt5 中QSS_第4张图片

你可能感兴趣的:(零基础学Python)