【问题标题】:How to make window fade out slowly when I click the close button by PyQt5?当我点击PyQt5的关闭按钮时,如何使窗口慢慢淡出?
【发布时间】:2018-03-07 19:39:33
【问题描述】:

这里是代码。我的想法是当我点击按钮时,窗口会从下到上慢慢变透明,然后窗口的透明度为0,最后窗口关闭,但我不知道该怎么做。

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import (QWidget,QPushButton,QVBoxLayout,QApplication)

class Window(QWidget):
  def __init__(self, parent = None):   
    QWidget.__init__(self, parent)

    button = QPushButton(self.tr("Click me!"))

    button.clicked.connect(self.fade)

    layout = QVBoxLayout(self)
    layout.addWidget(button)

def fade(self):   
    self.setWindowOpacity(0.2)
    QTimer.singleShot(5000, self.unfade)#it does not work
    self.close()

def unfade(self):
    self.setWindowOpacity(1)


if __name__ == "__main__":

app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())

【问题讨论】:

  • 试试下面的代码:gist.github.com/eyllanesc/6a582ec1db6e7e89a8da24ceab76a1e7 告诉我它缺少什么来满足你的要求。
  • 谢谢。这就是我想要的效果,但是按钮如何连接关闭事件呢?我的意思是当我单击按钮时,窗口会以这种方式关闭
  • 请解释清楚。
  • 那么,你想让它在你关闭窗口时运行,也就是在你按下按钮的时候,当它完成时,它会关闭?
  • 正确。这就是我的意思。

标签: python qt pyqt python-3.5 pyqt5


【解决方案1】:

您想要的效果我们可以使用setMask() 方法创建它,该方法仅使小部件部分对区域可见,因此我们必须改变区域的高度,我们将使用类QPropertyAnimation,但是对于这我们必须通过pyqtProperty 创建一个属性。该任务将在closeEvent()方法中启动,如下所示:

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_())

【讨论】:

  • 你好,有时候淡出不慢,直接关闭。如何解决这个问题?
【解决方案2】:

可以通过调整fade函数的time.sleep()值来调整淡出速度。

它没有真正的动画那么流畅,但它会很有用。

import sys
import time
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QVBoxLayout


class FadeWindow(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)

        button = QPushButton(self)
        button.setText("Close")

        button.resize(200, 200)
        button.clicked.connect(self.fade)

        layout = QVBoxLayout(self)
        layout.addWidget(button)

        self.resize(300, 300)

    def fade(self):
        for i in range(10):
            i = i / 10
            self.setWindowOpacity(1 - i)
            time.sleep(0.05)
        self.close()

    def close(self):
        sys.exit()


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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    • 2021-03-28
    • 2013-02-10
    • 1970-01-01
    相关资源
    最近更新 更多