关闭带有效果淡入淡出窗口pyqt5时的错误

时间:2018-11-08 16:06:16

标签: python pyqt5

尝试更改效果淡出的新窗口UI时,我有些担心。我在mainwindow的closeEvent上添加了效果,但是它不起作用。

这是我的代码:

使用的图书馆:

shallow()

加载用户界面

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5 import uic

班级首页:

uifile_1 = 'home.ui'
form_1, base_1 = uic.loadUiType(uifile_1)

uifile_2 = 'plate.ui'
form_2, base_2 = uic.loadUiType(uifile_2)

车牌页

class HomePage(base_1, form_1):
    def __init__(self):
        super(base_1,self).__init__()
        self.setupUi(self)

        #add button for click next page
        self.btn_start = QPushButton(self)
        self.btn_start.clicked.connect(self.change)

        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

    def change(self):
        self.plate = PlatePage()
        self.plate.show()
        self.close()

    @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:   
            self.closeEvent(self, event)   

请看看并给我一些解决方案。

谢谢

1 个答案:

答案 0 :(得分:0)

尝试一下:

from PyQt5.QtCore    import QPropertyAnimation, QThread
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QPushButton

class Window(QWidget):

    def __init__(self, *args, **kwargs):
        super(Window, self).__init__(*args, **kwargs)
        self.resize(400, 400)
        layout = QVBoxLayout(self)
        layout.addWidget(QPushButton('Button', self))

        self.animation = QPropertyAnimation(self, b'windowOpacity')
        self.animation.setDuration(1000)        
        self.isStarted = False 

        self.doShow()

    def doShow(self):
        try:
            self.animation.finished.disconnect(self.close)
        except:
            pass

        self.animation.stop()
        self.animation.setStartValue(0)
        self.animation.setEndValue(1)
        self.animation.start()

    def closeEvent(self, event):
        if not self.isStarted:
            self.animation.stop()
            self.animation.finished.connect(self.close)  
            self.animation.setStartValue(1)
            self.animation.setEndValue(0)
            self.animation.start()
            self.isStarted = True
            event.ignore()
        else:
            event.accept()

if __name__ == '__main__':
    import sys
    from PyQt5.QtWidgets import QApplication
    app = QApplication(sys.argv)
    w = Window()
    w.show()
    sys.exit(app.exec_())

enter image description here