Fish in Qt
py
Python
#!/usr/bin/python3
from PyQt5.QtCore import Qt, QPoint
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QPainter, QPixmap, QBitmap, QColor
MAX_SIZE = 250
class FishWindow(QWidget):
def __init__(self):
super().__init__()
fishPixmap = self.loadFish("fish.png")
self.setFixedSize(fishPixmap.size())
# fishMask = fishPixmap.createMaskFromColor(QColor(0, 0, 0, 0), Qt.MaskOutColor)
fishMask = fishPixmap.createMaskFromColor(QColor(0, 0, 0, 0), Qt.MaskInColor)
# fishMask = fishPixmap.createHeuristicMask()
self.setMask(fishMask)
self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowMaximizeButtonHint)
self.setAttribute(Qt.WA_TranslucentBackground, True)
self.dragPosition = QPoint(0, 0)
self.fishPixmap = fishPixmap
self.windows = list()
def loadFish(self, fileName):
fishPixmap = QPixmap("fish.png")
fishSize = fishPixmap.size()
if fishSize.height() > fishSize.width():
height = MAX_SIZE
width = int(MAX_SIZE * fishSize.width() / fishSize.height())
else:
width = MAX_SIZE
height = int(MAX_SIZE * fishSize.height() / fishSize.width())
fishPixmap = fishPixmap.scaled(width, height, Qt.KeepAspectRatio)
return fishPixmap
def paintEvent(self, event):
painter = QPainter(self)
painter.drawPixmap(0, 0, self.fishPixmap)
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
event.accept()
self.newFish()
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
# self.dragPosition = event.globalPos() - self.frameGeometry().topLeft()
self.dragPosition = self.mapFromGlobal(event.globalPos())
event.accept()
def mouseMoveEvent(self, event):
if event.button() == 0:
self.move(event.globalPos() - self.dragPosition)
event.accept()
def newFish(self):
window = FishWindow()
window.move(QPoint(500, 500))
window.show()
self.windows.append(window)
if __name__ == '__main__':
app = QApplication([])
fishWindow = FishWindow()
fishWindow.show()
app.exec_()