Introduction to PyQt5 (26) Alien Window

table of Contents

1. Realize irregular windows (alien windows)

 2. Move and close irregular windows (alien windows)

Three. Realize the animation effect of the special-shaped window


1. Realize irregular windows (alien windows)

Realize special-shaped window through mask

A transparent png image is needed, and the transparent part is deducted to form a non-rectangular area

 

Code:

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class AbnormityWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("异形窗口")
        self.pix = QBitmap('../picture/images/mask.png')
        self.resize(self.pix.size())
        #设置掩膜,窗口就是掩膜的形状
        self.setMask(self.pix)

    def paintEvent(self, event):
        painter=QPainter(self)
        painter.drawPixmap(0,0,self.pix.width(),self.pix.height(),QPixmap('../picture/images/screen1.jpg'))


if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = AbnormityWindow()
    form.show()
    sys.exit(app.exec_())

operation result:

The visible window becomes the shape of the mask.

Mask picture:      Window picture:    

Run out of the window:

 

 2. Move and close irregular windows (alien windows)

Coordinate system in pyqt5

Code:

Combine the code to see the moving distance

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *


class AbnormityWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("异形窗口")
        self.pix = QBitmap('../picture/images/mask.png')
        self.resize(self.pix.size())
        self.setMask(self.pix)

    #鼠标按下
    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            #鼠标按下或抬起标志位
            self.m_drag = True
            #当前单击点相对于窗口本身的坐标,永远是正的
            self.m_DragPosition = event.globalPos() - self.pos()
            #print(self.m_DragPosition)
            #设置光标形状
            self.setCursor(QCursor(Qt.OpenHandCursor))
            '''
            # 当前单击点相对于屏幕的坐标,包括标题栏和边框
            print(event.globalPos())
            # 当前单击点相对于窗口的坐标,忽略标题栏和边框
            print(event.pos())
            '''
            # 左上角坐标相对于屏幕的坐标,包括标题栏和边框
            print(self.pos())
        #按下右键
        if event.button() == Qt.RightButton:
            self.close()


    #鼠标移动
    def mouseMoveEvent(self, QMouseEvent):
        if Qt.LeftButton and self.m_drag:
            # 当左键移动窗体修改偏移值
            # QPoint
            '''
            实时计算窗口左上角坐标,注意是左上角!!!!!!
            这个移动是从上次的窗口位置往现在到达的位置移动,
            所以是现在的globalPos()减去移动之前的单击点到窗口边框的距离,就是当前左上角坐标
            '''
            self.move(QMouseEvent.globalPos() - self.m_DragPosition)


    #鼠标抬起
    def mouseReleaseEvent(self, QMouseEvent):
        self.m_drag = False
        #cursor(n.)光标
        self.setCursor(QCursor(Qt.ArrowCursor))


    def paintEvent(self, event):
        painter = QPainter(self)
        painter.drawPixmap(0,0,self.pix.width(),self.pix.height(),QPixmap('../picture/images/screen1.jpg'))


if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = AbnormityWindow()
    form.show()
    sys.exit(app.exec_())

operation result:

The interface is the same as above, that is, you can drag by pressing the left button, and you can close the window by clicking the right button.

 

Three. Realize the animation effect of the special-shaped window

This one is a bit confusing, I will take a break first.

Code:

import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QPixmap, QPainter, QCursor
from PyQt5.QtCore import Qt, QTimer


class AnimationWindows(QWidget):
    def __init__(self, parent=None):
        super(AnimationWindows, self).__init__(parent)
        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):
        #通过立即调用paintEvent()来直接重新绘制窗口部件
        self.update()
        if self.i == 5:
            self.i = 1
        self.mypic = {1: '../picture/images/left.png', 2: "../picture/images/up.png", 3: '../picture/images/right.png', 4: '../picture/images/down.png'}
        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))

    def mouseMoveEvent(self, QMouseEvent):
        if Qt.LeftButton and self.m_drag:
            self.move(QMouseEvent.globalPos() - self.m_DragPosition)

    def mouseReleaseEvent(self, QMouseEvent):
        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)
    form = AnimationWindows()
    form.show()
    sys.exit(app.exec_())

operation result:

This thing keeps spinning. Try it yourself.

 

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_44593822/article/details/113835209