pyqt 按钮常用格式Qss设置

pyqt 按钮常用格式Qss设置

QSS介绍

Qt Style Sheets (QSS) 是 Qt 框架中用于定制应用程序界面样式的一种语言。它类似于网页开发中的 CSS(Cascading Style Sheets),但专门为 Qt 应用程序设计。使用 QSS,你可以控制应用程序中各种元素的外观,包括颜色、字体、边框、边距、背景图像等。

按钮常用的QSS设置

background-color: #4CAF50; /* 绿色背景 /
color: white; /
白色文字 /
border: none; /
无边框 /
border-radius: 5px; /
边框圆角 /
padding: 10px 20px; /
内边距 /
font-size: 16px; /
字体大小 /
font-family: "Arial"; /
字体类型,使用 Arial 或其他你想要的字体 /
font-weight: bold; /
加粗效果 /
transition: background-color 0.3s ease-in-out; /
平滑过渡效果 */

}

复制代码
    QPushButton:hover {  
        /* 鼠标悬停效果 */  
        background-color: #45a049; /* 更深的绿色背景 */  
    }  

    /* 注意:QPushButton:pressed 的样式需要编程实现 */  

效果


代码

python 复制代码
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QHBoxLayout, QGridLayout
from PyQt5.QtCore import Qt


class CenteredButton(QWidget):
    def __init__(self):
        super().__init__()

        # 创建一个按钮
        self.button = QPushButton('Click Me', self)

        # 使用水平布局并添加伸缩因子来实现居中
        layout = QHBoxLayout(self)
        layout.addStretch(1)  # 在按钮左边添加伸缩因子
        layout.addWidget(self.button)  # 添加按钮
        layout.addStretch(1)  # 在按钮右边添加伸缩因子

        # 设置窗口的布局
        self.setLayout(layout)

        # 设置窗口的标题和大小
        self.setWindowTitle('Centered Button')
        self.setGeometry(600, 300, 400, 300)  # x, y, width, height
        # QSS 样式
        style_sheet = """  
        QPushButton {  
            /* 基本设置 */  
            background-color: #4CAF50; /* 绿色背景 */  
            color: white; /* 白色文字 */  
            border: none; /* 无边框 */  
            border-radius: 5px; /* 边框圆角 */  
            padding: 10px 20px; /* 内边距 */  
            font-size: 16px; /* 字体大小 */  
            font-family: "Arial"; /* 字体类型,使用 Arial 或其他你想要的字体 */  
            font-weight: bold; /* 加粗效果 */  
            transition: background-color 0.3s ease-in-out; /* 平滑过渡效果 */  
        }  

        QPushButton:hover {  
            /* 鼠标悬停效果 */  
            background-color: #45a049; /* 更深的绿色背景 */  
        }  

        /* 注意:QPushButton:pressed 的样式需要编程实现 */  
        """

        # 应用样式表
        self.button.setStyleSheet(style_sheet)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    centered_button = CenteredButton()
    centered_button.show()
    sys.exit(app.exec_())
相关推荐
Humbunklung1 天前
PySide6 GUI 学习笔记——常用类及控件使用方法(多行文本控件QTextEdit)
笔记·python·学习·pyqt
En^_^Joy2 天前
PyQt常用控件的使用:QFileDialog、QMessageBox、QTreeWidget、QRadioButton等
开发语言·python·pyqt
zhlei_123453 天前
封闭内网安装配置VSCode Anconda3 并配置 PyQt5开发
ide·vscode·pyqt
猫头虎4 天前
零基础安装 Python 教程:从下载到环境配置一步到位(支持 VSCode 和 PyCharm)与常用操作系统操作指南
vscode·python·pycharm·beautifulsoup·numpy·pyqt·pip
江畔柳前堤12 天前
PyQt学习系列08-插件系统与模块化开发
运维·开发语言·数据库·python·学习·机器学习·pyqt
江畔柳前堤15 天前
PyQt学习系列05-图形渲染与OpenGL集成
开发语言·javascript·人工智能·python·学习·ecmascript·pyqt
江畔柳前堤15 天前
PyQt学习系列11-综合项目:多语言文件管理器
开发语言·网络·python·学习·django·pyqt
幽络源小助理16 天前
基于Yolov8+PyQT的老人摔倒识别系统源码
yolo·pyqt
江畔柳前堤16 天前
PyQt学习系列07-数据库操作与ORM集成
数据库·学习·算法·机器学习·架构·pyqt
江畔柳前堤16 天前
PyQt学习系列10-性能优化与调试技巧
开发语言·javascript·数据库·学习·性能优化·ecmascript·pyqt