- 前言
- 一、QtDesigner内的编程
- 1. 按钮变化
- 2.边框和圆角
- 3.无边框和按住指令
- 4.指定圆角和插入图片(拉伸和堆叠)
- 5.只有下划线的透明输入框
- 二、py文件内的编程
- 1.创建无边框效果
- 2.创建阴影效果
- 3.无边框拖动效果
- 4.无边框的最大化
前言
自用的pyqt5界面优化美化代码,方便自己写界面时复制,也顺便造福广大网友吧!
首先安装基础工具:
Pycharm配置QtDesigner(PyUIC、PyRcc)
文章将持续更新.......
一、QtDesigner内的编程 1. 按钮变化
#pushButton_create_user{
background-color: rgb(0, 0, 0);
color: rgb(255, 255, 255);
border: 3px solid rgb(0,0,0);
border-radius:10px
}
#pushButton_create_user:hover{
color: rgb(0, 0, 0);
background-color: rgb(255, 255, 255);
}
#pushButton_create_user:pressed{
padding-top:5px;
padding-left:5px
}
鼠标不置于其上效果:
鼠标置于其上但不点击效果:
点击效果(字体往右下角移动5px):
border:2px solid rgb(186,186,186); border-radius:10px
效果:
#pushButton_login{
border:none;
}
#pushButton_login:focus{
color:rgb(186,186,186);
}
按住效果:
松开效果:
border-image: url(:/images/001.jpg); border-top-left-radius:30px; border-bottom-left-radius:30px
效果:
background-image: url(:/images/001.jpg); border-top-left-radius:30px; border-bottom-left-radius:30px5.只有下划线的透明输入框
border:none; border-bottom: 5px solid rgba(0,0,0,255); background-color: rgba(0,0,0,0);
效果:
二、py文件内的编程 1.创建无边框效果
MainWindow.setWindowFlags(QtCore.Qt.FramelessWindowHint) MainWindow.setAttribute(QtCore.Qt.WA_TranslucentBackground)
添加至开头:
效果(结合2的图片来看):
2.创建阴影效果
self.label.setGraphicsEffect(QtWidgets.QGraphicsDropShadowEffect(blurRadius=25, xOffset=0, yOffset=0)) self.pushButton_login.setGraphicsEffect(QtWidgets.QGraphicsDropShadowEffect(blurRadius=25, xOffset=3, yOffset=3))
添加至末尾:
效果:
3.无边框拖动效果
def mousePressEvent(self, event):
if event.button() == QtCore.Qt.LeftButton and self.isMaximized() == False:
self.m_flag = True
self.m_position = event.globalPos() - self.pos()
event.accept()
self.setCursor(QtGui.QCursor(QtCore.Qt.OpenHandCursor))
def mouseMoveEvent(self, mouse_event):
if QtCore.Qt.LeftButton and self.m_flag:
self.move(mouse_event.globalPos() - self.m_position)
mouse_event.accept()
def mouseReleaseEvent(self, mouse_event):
self.m_flag = False
self.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
添加至调用类的设定中:
效果(无按钮和框的位置都可以进行长按移动):
4.无边框的最大化
首先先规范创建可自由变化的UI
参考:



