短暂闪现一张图片

2024-06-12 11:41:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图修改一个用pyQt(特别是Anki)编写的程序。我希望程序能短暂地闪现一张图片(存储在我的硬盘上),然后继续正常运行。在

此代码将插入程序中的任意点。这是对现有程序的临时单用户修补程序-它不需要快速、优雅或易于维护。

我的问题是我对pyQt知之甚少。我需要定义一个全新的“窗口”吗,还是可以只运行某种“通知”函数,其中包含图像?在


Tags: 函数代码图像程序定义图片pyqt单用户
1条回答
网友
1楼 · 发布于 2024-06-12 11:41:14

QSplashScreen将对此很有用。它主要用于在程序加载时显示某些图像/文本,但您的案例看起来也非常适合于此。你可以通过点击它来关闭它,或者你还可以设置一个定时器在一段时间后自动关闭它。在

下面是一个有一个按钮的对话框的简单示例。按下后,它将显示图像并在2秒后关闭:

import sys
from PyQt4 import QtGui, QtCore

class Dialog(QtGui.QDialog):
    def __init__(self, parent=None):
        super(Dialog, self).__init__(parent)

        layout = QtGui.QVBoxLayout()
        self.setLayout(layout)

        self.b1 = QtGui.QPushButton('flash splash')
        self.b1.clicked.connect(self.flashSplash)

        layout.addWidget(self.b1)

    def flashSplash(self):
        # Be sure to keep a reference to the SplashScreen
        # otherwise it'll be garbage collected
        # That's why there is 'self.' in front of the name
        self.splash = QtGui.QSplashScreen(QtGui.QPixmap('/path/to/image.jpg'))

        # SplashScreen will be in the center of the screen by default.
        # You can move it to a certain place if you want.
        # self.splash.move(10,10)

        self.splash.show()

        # Close the SplashScreen after 2 secs (2000 ms)
        QtCore.QTimer.singleShot(2000, self.splash.close)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)

    main = Dialog()
    main.show()

    sys.exit(app.exec_())

相关问题 更多 >