PyQt5 实现异形窗口的动画效果 案例 自己准备4张png图片作为关键帧图片
素材:Black.png
代码:
import sys
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QIcon, QBitmap, QPainter, QPixmap, QCursor
from PyQt5.QtWidgets import QHBoxLayout, QPushButton, QMessageBox, QApplication, QVBoxLayout, QWidget, \
QLabel, QGridLayout, QLineEdit, QTextEdit, QFormLayout, QComboBox
'''
PyQt5 实现异形窗口的动画效果 案例
自己准备4张png图片作为关键帧图片
'''
class AnimationWindowDemo(QWidget):
def __init__(self,parent = None):
super(AnimationWindowDemo,self).__init__(parent)
self.initUI()
def initUI(self):
# 设置定位和左上角坐标
self.setGeometry(300, 300, 360, 260)
# 设置窗口标题
self.setWindowTitle('异形窗口 的演示')
# 设置窗口图标
# self.setWindowIcon(QIcon('../web.ico'))
self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
self.setStyleSheet('''background-color:black; ''')
self.i = 1
self.mypix()
self.timer = QTimer()
self.timer.setInterval(500) # 执行周期500毫秒
self.timer.timeout.connect(self.timeChange)
self.timer.start()
# 显示不规则pic
def mypix(self):
self.update()
if self.i == 5:
self.i = 1
self.mypic = {
1:'Black.png',
2:'../web.ico',
3:'Black.png',
4:'../web.ico'
}
self.pix = QPixmap(self.mypic[self.i])
self.resize(self.pix.size())
self.setMask(self.pix.mask())
self.dragPosition = None
def mousePressEvent(self,event):
if event.button() == Qt.LeftButton:
self.m_drag = True
self.m_DragPosition = event.globalPos() - self.pos()
self.setCursor(QCursor(Qt.OpenHandCursor))
print(event.globalPos())
print(event.pos())
print(self.pos())
if event.button() == Qt.RightButton:
self.close()
def mouseMoveEvent(self,event):
if Qt.LeftButton and self.m_drag:
# 当左键移动窗体修改偏移值 QPoint
# 实时计算窗口左上角坐标
self.move(event.globalPos() - self.m_DragPosition)
def mouseReleaseEvent(self,event):
if self.m_drag == False :
self.setCursor(QCursor(Qt.ArrowCursor))
def paintEvent(self,event):
painter = QPainter(self)
painter.drawPixmap(0,0,self.pix.width(),self.pix.height(),self.pix)
# 鼠标双击事件
def mouseDoubleClickEvent(self,event):
if event.button() == 1:
self.i += 1
self.mypix()
# 每500毫秒修改paint
def timeChange(self):
self.i += 1
self.mypix()
if __name__ == '__main__':
app = QApplication(sys.argv)
# 设置应用图标
app.setWindowIcon(QIcon('../web.ico'))
w = AnimationWindowDemo()
w.show()
sys.exit(app.exec_())