python -- pyQt5中 样式设置(边框、按钮、CSS 颜色代码)

设置Title标题

在这里插入图片描述

import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QWidget
 
class Logo(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
 
    def initUI(self):
        # self.setGeometry(300, 300, 300, 220)
        self.setWindowTitle('MyLogo')
        # self.move(300, 300)
        self.show()
 
if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = Logo()
    sys.exit(app.exec_())

一、父控件设置样式表后对子控件产生影响,控制styleSheet的作用范围

https://blog.csdn.net/qq_31073871/article/details/90288625

QFrame 作为容器,放入其他多种部件,里面的边框都生效
在这里插入图片描述

在类名后面用 #号串接变量名,子控件不受影响,达到预期效果在这里插入图片描述

二、border

border属性:设置元素的边框样式。可同时设置边框宽度、边框样式、边框颜色。也可单独设置上边、右边、下边、左边的边框。

border-width:边框宽度。可以指定长度值。如1px,1em(单位为px,pt,em等)。或者使用关键字medium(默认),thick,thin。
 border-top-width:设置元素上边框宽度
 border-right-width:设置元素右边框宽度
 border-bottom-width:设置元素下边框宽度
 border-left-width:设置元素左边框宽度

border-style:边框样式。
 border-top-style:设置元素上边框样式
 border-right-style:设置元素右边框样式
 border-bottom-style:设置元素下边框样式
 border-left-style:设置元素左边框样式

参考:多种样式例子 https://www.jianshu.com/p/54283902d4f7

边框样式(border-style)

none: 无样式;
hidden: 同样是无样式,主要用于解决和表格的边框冲突;
dotted: 点划线;
dashed: 虚线;
solid: 实线;
double: 双线,两条线加上中间的空白等于border-width的取值;
groove: 槽状;
ridge: 脊状,和groove相反;
inset: 凹陷;
outset:凸出,和inset相反;

border :1px solid black;  # 边框黑实线
例子 1
border-style:dotted solid double dashed;
上边框是点状
右边框是实线
下边框是双线
左边框是虚线

例子 2
border-style:dotted solid double;
上边框是点状
右边框和左边框是实线
下边框是双线

例子 3
border-style:dotted solid;
上边框和下边框是点状
右边框和左边框是实线

例子 4
border-style:dotted;
所有 4 个边框都是点状

在这里插入图片描述

三、pushButton 灯设置

3.1 代码控制

self.result_UI.pushButton_11.setStyleSheet(
            '''min-width: 16px; min-height: 16px;max-width:16px; max-height: 16px;border-radius: 8px;  border:1px 
            solid black;background:red;}''')  # 亮红
        self.result_UI.pushButton_12.setStyleSheet(
            '''min-width: 16px; min-height: 16px;max-width:16px; max-height: 16px;border-radius: 8px;  border:1px 
            solid black;background-color: rgb(58, 111, 50);}''')  # 暗绿

self.System_UI.pushButton_6.setEnabled(False) # 是否启用

在这里插入图片描述

    def red(self):
        self.result_UI.pushButton_11.setStyleSheet(
            '''min-width: 16px; min-height: 16px;max-width:16px; max-height: 16px;border-radius: 8px;  border:1px 
            solid black;background:red;}''')  # 亮红
        self.result_UI.pushButton_11.setStyleSheet(
            '''min-width: 16px; min-height: 16px;max-width:16px; max-height: 16px;border-radius: 8px;  border:1px 
            solid black;background-color: rgb(58, 111, 50);}''')  # 暗绿

    def green(self):
        self.result_UI.pushButton_11.setStyleSheet(
            '''min-width: 16px; min-height: 16px;max-width:16px; max-height: 16px;border-radius: 8px;  border:1px 
            solid black;background-color: rgb(0, 234, 0);}''')  # 亮绿
        self.result_UI.pushButton_11.setStyleSheet(
            '''min-width: 16px; min-height: 16px;max-width:16px; max-height: 16px;border-radius: 8px;  border:1px 
            solid black; background-color: rgb(117, 0, 0);}''')  # 暗红

3.2 QT Designer 设置

min-width: 16px; min-height: 16px;max-width:16px; max-height: 16px;border-radius: 8px;  border:1px solid black;background-color: rgb(144, 144, 144);

在这里插入图片描述

四、CSS 样式颜色对照表

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 12
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以通过CSS样式表来设置Qt应用程序的菜单样式。以下是一个示例,展示如何设置菜单栏和弹出菜单的样式: ```python from PyQt5.QtGui import * from PyQt5.QtCore import * from PyQt5.QtWidgets import * class MenuExample(QMainWindow): def __init__(self): super().__init__() menubar = self.menuBar() # 创建菜单 fileMenu = menubar.addMenu('文件') editMenu = menubar.addMenu('编辑') viewMenu = menubar.addMenu('视图') # 创建菜单项 newAction = QAction('新建', self) newAction.setShortcut('Ctrl+N') fileMenu.addAction(newAction) saveAction = QAction('保存', self) saveAction.setShortcut('Ctrl+S') fileMenu.addAction(saveAction) exitAction = QAction('退出', self) exitAction.setShortcut('Ctrl+Q') exitAction.triggered.connect(self.close) fileMenu.addAction(exitAction) # 设置菜单样式 menubar.setStyleSheet(''' QMenuBar { background-color: #3d3d3d; color: #fff; font-size: 14px; } QMenuBar::item { spacing: 3px; padding: 1px 4px; background-color: transparent; border-radius: 4px; } QMenuBar::item:selected { background-color: #2d2d2d; } QMenu { background-color: #f0f0f0; border: 1px solid #555; margin: 2px; } QMenu::item { padding: 2px 20px 2px 20px; } QMenu::item:selected { background-color: #555; color: #fff; } ''') # 显示窗口 self.setGeometry(300, 300, 300, 200) self.setWindowTitle('菜单示例') self.show() if __name__ == '__main__': app = QApplication([]) example = MenuExample() app.exec_() ``` 在这个例子,我们使用了QMenuBar、QMenu和QAction来创建菜单栏和菜单项。然后我们使用setStyleSheet()方法来设置菜单的样式。在这个样式,我们设置了菜单栏的背景颜色、字体大小和菜单项的间距和边框半径。我们还设置了选菜单项时的背景颜色和文字颜色。 您可以根据需要修改样式表来满足您的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值