Last active
September 27, 2017 04:44
-
-
Save eyllanesc/6a582ec1db6e7e89a8da24ceab76a1e7 to your computer and use it in GitHub Desktop.
Possible solution of https://stackoverflow.com/questions/46428856/how-to-make-window-fade-out-slowly-when-i-click-the-close-button-by-pyqt5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from PyQt5.QtCore import * | |
from PyQt5.QtGui import * | |
from PyQt5.QtWidgets import * | |
class FadeWidget(QWidget): | |
def __init__(self, parent = None): | |
QWidget.__init__(self, parent) | |
self._heightMask = self.height() | |
self.animation = QPropertyAnimation(self, b"heightPercentage") | |
self.animation.setDuration(1000) | |
self.animation.setStartValue(self.height()) | |
self.animation.setEndValue(-1) | |
self.animation.finished.connect(self.close) | |
self.isStarted = False | |
@pyqtProperty(int) | |
def heightMask(self): | |
return self._heightMask | |
@heightMask.setter | |
def heightPercentage(self, value): | |
self._heightMask = value | |
rect = QRect(0, 0, self.width(), self.heightMask) | |
self.setMask(QRegion(rect)) | |
def closeEvent(self, event): | |
if not self.isStarted: | |
self.animation.start() | |
self.isStarted = True | |
event.ignore() | |
else: | |
QWidget.closeEvent(self, event) | |
if __name__ == "__main__": | |
app = QApplication(sys.argv) | |
window = FadeWidget() | |
window.show() | |
sys.exit(app.exec_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment