Lv.的博客

PyQt5 qss样式

复制代码
################################
# PyQt5中文网 - PyQt5全套视频教程 #
#    https://www.PyQt5.cn/     #
#         主讲: 村长            #
################################

from PyQt5.Qt import *
import sys


class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("初识QSS样式加载方法 - PyQt5中文网")
        self.resize(600, 500)
        self.func_list()

    def func_list(self):
        self.func()

    def func(self):
        # 局部设置
        label1 = QLabel('标签1', self)
        # label1.setStyleSheet('background-color:green;')
        label1.move(100, 200)
        label1.resize(70, 30)

        label2 = QLabel('标签2', self)
        # label2.setStyleSheet('color:red;')
        label2.move(200, 200)
        label2.resize(70, 30)

        label3 = QLabel('标签3', self)
        # label3.setStyleSheet('background-color:yellow;font-size:30px;')
        label3.move(300, 200)
        label3.resize(70, 30)

        # 全局设置
        btn1 = QPushButton('按钮1', self)
        btn1.move(100, 100)
        btn1.resize(70, 30)
        btn2 = QPushButton('按钮2', self)
        btn2.move(200, 100)
        btn2.resize(70, 30)

        # btn1.setStyleSheet('''
        #     QPushButton{
        #             color:red;
        #         }
        #     QPushButton:hover{
        #             background-color: green;
        #         }
        # ''')

        # QSS文件加载
        btn3 = QPushButton('按钮3', self)
        btn3.setProperty('name', 'btn')
        btn3.move(100, 300)
        btn3.resize(70, 30)
        btn4 = QPushButton('按钮4', self)
        btn4.setObjectName('btn')
        btn4.move(200, 300)
        btn4.resize(70, 30)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()

    with open('index.qss', 'r', encoding='UTF-8') as f:
        app.setStyleSheet(f.read())

    # QssStyle = '''
    #         QPushButton:hover{
    #                 background-color: green;
    #             }
    #         QPushButton[name="btn"]:hover{
    #                 background-color: red;
    #             }
    #         QPushButton#btn:hover{
    #                 background-color: green;
    #                 color:red;
    #             }
    #         '''
    # # 全局设置
    # window.setStyleSheet(QssStyle)  # 当前窗口全局有效

    window.show()
    sys.exit(app.exec_())
复制代码
复制代码
QPushButton:hover{
                     background-color: green;
}
QPushButton[name="btn"]:hover{
                     background-color: red;
}
QPushButton#btn:hover{
                     background-color: green;
                    color:red;
}
复制代码

 

posted @   Avatarx  阅读(229)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2016-05-31 SQL server插入数据后,获取自增长字段的值
2016-05-31 sqlserver 创建用户 sp_addlogin
2016-05-31 存储过程嵌套模板
2014-05-31 linux安装qwt插件linux
2014-05-31 qt超强绘图控件qwt - 安装及配置
2014-05-31 WIN7 下 Qt Creator 安装 QWT
点击右上角即可分享
微信分享提示