Search code examples
nullpyqt5pyinstallerpy2appqpixmap

pyqt5 py2app/pyinstaller MacOS High Sierra QPixmap::scaled: Pixmap is a null pixmap


I am using py2app on a pyqt5 script to develop a standalone application. The program must show a photo in the dialog. I use the script below and it works fine on Ubuntu and windows, both when using python and also when using pyinstaller standalone executables.

On MacOS High Sierra, however, when use python to run the scripts the image shows just fine. When I make executables using either py2app or pyinstaller, I get the famous QPixmap::scaled: Pixmap is a null pixmap error.

Here is the code for putting the image up:

pic = QLabel(Dialog)
height_label = 300
pic.resize(Dialog.width(), height_label)
pixmap = QtGui.QPixmap(
    os.path.join(os.getcwd(), "main.jpg"))
pic.setPixmap(pixmap.scaled(pic.size(), QtCore.Qt.IgnoreAspectRatio))

Solution

  • If the program needs specific data of images, it's better to specify the files in the py2app setup.py with DATA_FILES = ["main.jpg"].

    os.getcwd() would only redirect the executable directory if the executable is run from the commandline. Otherwise it will default to the system's home directory. This will work just fine:

        file = sys.argv[0]
        dirname = os.path.dirname(file)
        pixmap = QtGui.QPixmap(
            os.path.join(dirname, "main.jpg"))