Qt启动画面未在python中显示

时间:2015-09-21 10:47:13

标签: python linux qt

我正在尝试为我的程序添加一个启动画面,用python 2.7编码并使用pyqt4库。我的主要文件是:

#!/usr/bin/env python

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from logindialog import LoginDialog
from mainwindow import MainWindow
import time
import sys


if __name__ == "__main__":
    try:
        app = QApplication(sys.argv)

        mw = MainWindow()

        # Create and display the splash screen
        splash_pix = QPixmap('images/sherlock_splash.png')
        splash = QSplashScreen(splash_pix, Qt.WindowStaysOnTopHint)
        splash.setMask(splash_pix.mask())

        # adding progress bar
        progressBar = QProgressBar(splash)
        # adding message
        splash.showMessage('Discovering Nodes...', Qt.AlignRight | Qt.AlignBottom, Qt.darkRed)

        splash.show()
        app.processEvents()

        for i in range(0, 100):
            progressBar.setValue(i)
            # Simulate something that takes time
            time.sleep(0.1)

        splash.close()

        # Show main window
        mw.show()
        app.exec_()
    except Exception:
        sys.exit(1)
    sys.exit(0)

我使用Pycharm IDE对其进行了编码。如果我使用pycharm RUN功能运行它,则会正确显示启动画面,但是如果我在linux命令行(./main.py)中运行它,则在启动应用程序时它不会显示启动画面。

有人可以帮助我吗?

非常感谢!

更新& FIX

...
# Create and display the splash screen
image_path = os.path.dirname(os.path.realpath(__file__))
splash_pix = QPixmap('/'.join([image_path, 'images/sherlock_splash.png']))
splash = QSplashScreen(splash_pix, Qt.WindowStaysOnTopHint)
...

谢谢!

1 个答案:

答案 0 :(得分:2)

检查项目结构并确定.png文件的相对路径是否正确

从命令行运行时

'images/sherlock_splash.png'

同时添加以下检查

if splash_pix is not None:
    ...