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_())
相关推荐
云空2 天前
《PyQtGraph例子库:Python数据可视化的宝藏地图》
开发语言·python·信息可视化·scikit-learn·pyqt
想成为风筝4 天前
从零开始学习深度学习—水果分类之PyQt5App
人工智能·深度学习·计算机视觉·pyqt
云空5 天前
《PyQt6-3D:开启Python 3D开发新世界》
python·3d·pyqt
云空6 天前
《QtPy:Python与Qt的完美桥梁》
开发语言·python·qt·pyqt
云空8 天前
《PyQt6-3D应用开发技术文档》
3d·pyqt
sword devil9008 天前
PYQT实战:无刷电机模拟(只是模拟,没有写接口接收外部数据)
pyqt
sword devil90011 天前
PYQT实战:智能家居中控
python·智能家居·pyqt
OICQQ6765800815 天前
创建一个基于YOLOv8+PyQt界面的驾驶员疲劳驾驶检测系统 实现对驾驶员疲劳状态的打哈欠检测,头部下垂 疲劳眼睛检测识别
yolo·pyqt·疲劳驾驶·检测识别·驾驶员检测·打哈欠检测·眼睛疲劳
小灰灰搞电子24 天前
Qt PyQt与PySide技术-C++库的Python绑定
c++·qt·pyqt
越甲八千1 个月前
pyqt 简单条码系统
数据库·microsoft·pyqt