Images not showing when running a frozen pyqt app on another computer

后端 未结 3 1539
囚心锁ツ
囚心锁ツ 2020-12-10 19:12

I have a PyQt4 program that I froze using cx_freeze. The problem I am having is when I make a QGraphicsPixmapItem, which it is getting its\' pixmap made from a SVG file, th

相关标签:
3条回答
  • 2020-12-10 19:47

    This is a nasty problem I have run into myself in the past. Let me quote http://www.py2exe.org/index.cgi/Py2exeAndPyQt: (I know you are using cx_freeze but I am sure you can adapt your script)

    PyQt4 and image loading (JPG, GIF, etc)

    PyQt4 uses plugins to read those image formats, so you'll need to copy the folder PyQt4\plugins\imageformats to appdir\imageformats. Like in the above cases, you can use data_files for this. This won't work with bundle_files on.

    If the plugins are not reachable, then QPixmap.load/loadFromData will return False when loading an image in those formats.

    testapp.py:

    from PyQt4 import QtGui, QtSvg
    import sys
    
    app = QtGui.QApplication([])
    wnd = QtSvg.QSvgWidget()
    wnd.load("flower.svg")
    wnd.show()
    sys.exit(app.exec_())
    

    setup.py:

    from cx_Freeze import setup, Executable
    files = ['flower.svg']
    includes = ['sip', 'PyQt4.QtCore']
    setup(
            name = 'Example',
            version = '1.337',
            description = 'Allows user to see what I did there.',
            author = 'something',
            options = {'build_exe': {'include_files':files, 'includes':includes}},
            executables = [Executable('testapp.py')])
    

    I created this test app on a Windows 7 machine and copied it over to a Windows XP machine. I did not have to copy any dlls around - it worked just like that.

    0 讨论(0)
  • 2020-12-10 19:50

    I've added a hook to cx_freeze that includes imageformats whenever PyQt4.QtGui is included in the original code. With imageformats in the right place, even the externally stored icons work.

    https://bitbucket.org/anthony_tuininga/cx_freeze/pull-request/11/added-pyqt4qtgui-load-hook-that-adds/diff

    0 讨论(0)
  • 2020-12-10 20:04

    For people coming here from Google: if you only use QtWebKit, you do need to copy the imageformats dir (which you find in PYTHONDIR\lib\site-packages\PyQt4\plugins) into your app dir. Specifying PyQt4.QtWebKit among the includes is not enough.

    0 讨论(0)
提交回复
热议问题