QSplashScreen在PyQt4中无法正常工作

2024-04-24 07:35:24 发布

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

我试图用QSplashScreen制作一个闪屏,但是图像显示不正确。在

spalsh屏幕正常工作,但是splash_加载.png没有显示。在

此代码来自在线教程:

from PyQt4.QtCore import *
from PyQt4.QtGui import *


class Form(QDialog):
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)
        self.browser = QTextBrowser()
        self.setWindowTitle('Just a dialog')
        self.lineedit = QLineEdit("Write something and press Enter")
        self.lineedit.selectAll()
        layout = QVBoxLayout()
        layout.addWidget(self.browser)
        layout.addWidget(self.lineedit)
        self.setLayout(layout)
        self.lineedit.setFocus()
        self.connect(self.lineedit, SIGNAL("returnPressed()"),
                 self.update_ui)

    def update_ui(self):
        self.browser.append(self.lineedit.text())


if __name__ == "__main__":
    import sys, time

    app = QApplication(sys.argv)


    splash_pix = QPixmap('splash_loading.png')
    splash = QSplashScreen(splash_pix, Qt.WindowStaysOnTopHint)
    splash.setMask(splash_pix.mask())
    splash.show()
    app.processEvents()

    # Simulate something that takes time
    time.sleep(2)

    form = Form()
    form.show()
    splash.finish(form)
    app.exec_()

Splash Screen pyqt blank


Tags: fromimportselfbrowserformapptimepng
1条回答
网友
1楼 · 发布于 2024-04-24 07:35:24

你不应该使用睡眠,你必须处理所有事件。将app = ...form = Form()之间的行替换为:

start = time.time()
splash = QSplashScreen(QPixmap("screen.png"),  Qt.WindowStaysOnTopHint)
splash.show()
while time.time() - start < 2:
    time.sleep(0.001)
    app.processEvents()

enter image description here

相关问题 更多 >