UI转py代码:pyside2-uic main.ui > ui_main.py
参考学习文章:安装与运行 | 白月黑羽后台线程 与 信号 | 白月黑羽安装与运行 | 白月黑羽
一、去除QMainWinfow默认样式
1.去除菜单栏
2.移除状态栏
二、设置隐藏框
from PySide2 import QtCore
# 隐藏框
MainWindow.setWindowFlag(QtCore.Qt.FramelessWindowHint)
MainWindow.setAttribute(QtCore.Qt.WA_TranslucentBackground)
三、设置拖拽
from PySide2 import QtCore
from PySide2 import QtGui
# 设置拖拽
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))
四、设置放大缩小
from PySide2 import QtGui
#设置放大缩小
def restore_or_maxmize_window(self):
if self.isMaximized():
self.showNormal()
self.ui.pushButton.setIcon(QtGui.QIcon(u':/icons/icons/maxsize.png'))
else:
self.showMaximized()
self.ui.pushButton.setIcon(QtGui.QIcon(u':/icons/icons/maxsizeWhite.png'))
五、设置阴影
# 设置阴影
self.effect_shadow = QtWidgets.QGraphicsDropShadowEffect(self)
self.effect_shadow.setOffset(2,3)
self.effect_shadow.setBlurRadius(10)
self.effect_shadow.setColor(QtCore.Qt.black)
getattr(self.ui,i).setGraphicsEffect(self.effect_shadow)
六、绑定stackedWidget事件(切换界面)
self.ui.pushButton.clicked.connect(lambda: self.ui.stackedWidget.setCurrentIndex(0))
七、常用css样式
/* 去除按钮默认样式 */
QPushButton{
border: none;
background-color: rgba(0,0,0,0);
}
/* 设置按钮被鼠标点击后的背景颜色 */
QPushButton:focus{
background-color: rgb(161, 161, 161)
}
/* 设置悬浮在按钮上的背景颜色变化 */
QPushButton:hover{
background-color: rgb(161, 161, 161)
}