How to include icons in application when using Pyi

2019-05-08 00:44发布

问题:

what script looks like

what working app should look like

Before posting I have looked at the following question and tried to use it as a guide to make my script work properly but it was of marginal use

PyInstaller won't load the PyQt's images to the GUI

the best it did was include my icons in the resulting directory as follows

(icons included image here)

and the following one I have no idea what it is even saying but I feel it can solve my problem if I knew what it was actually doing, Bundling data files with PyInstaller (--onefile)

I have rtfm and most of it goes above my head unfortunately. The following section which I think pertains to my question

http://www.pyinstaller.org/wiki/Recipe/CollectDatafiles

I have no idea how to actually implemet it in my spec file

same goes for the following http://www.pyinstaller.org/export/v2.0/project/doc/Manual.html?format=raw#accessing-data-files

I have done the following and variations of it including using the -F parameter

I converted my mainWindow.ui file using pyside-uic.exe to convert it into mainGui.py file

then converted icons.qrc into icons_rc.py using the pyside-rcc.exe to make the conversion

(note: I converted the file after putting a copy of the exe originally in the PySide folder into my C:\Python27\Scripts folder which is in my Path so that I can call it from anywhere)

I store my icons in the icons folder

to make the executable in my console using Pyinstaller I have used the following command to make my spec file

C:\Pyinstaller-2.0> python utils\Makespec.py --windowed --name="16TangoTest" C:\Exmake\16MainWindowVideo.py

Once spec file is made I cut it from the pyinstaller folder and add it to my Exmake folder

then I modify it to include my icons folder using

Tree(C:\Exmake\icons), and append it to collect in the line above a.binaries

The manual and the first answer above indicate that I have to modify my spec file so that pyinstaller sees / finds my icons as I've done in the picture below

all it did was include my icons in the dist directory with the application still not displaying them as in the 1st picture above

Then I make the executable by running the following in the console C:\Pyinstaller-2.0>python pyinstaller.py C:\Exmake\16TangoTest.spec and that makes the folder that you see in the first picture, the icons are included but the folder but not the application when it is run

回答1:

Ok, final answer, it turns out that Qt needs the following file qico4.dll might be different for Qt5, when making your GUI in QtDesigner it automatically loads all the plugins it needs including the above image plug in, however when converting the file into a python script it does not include it unless you add the following code in your script

#:Loads the qico plug-in lets you use .ico icons
QPlugin = QPluginLoader("qico4.dll")

The text below might help others in different situations

This is a partial answer to my own question I would like to thank Blender up above, the short answer is you have to convert your icons from .ico into .png files, the problem is that you have to do the whole process of making your application again, these are the steps that I took I first went into the folder where I had my icons and converted them froom .ico into .png icons, I used the tool I got here http://www.towofu.net/soft/e-aicon.php I found this in this site..I love stackoverflow..ahem

once your icons are converted to the .png format you must change your .qrc file where you have your icon information and updated with the new icon information, I advise doing it from scratch; once you have your new icons.qrc file you then open your .ui file in QtDesinger and erase all the instances of your old icons from it and then proceed to update them with the newly formatted icons using the icon/resource file.

After all of your icons have been updated, you save it and continue with the conversion workflow, i.e convert your 'your_file.ui' to 'newly_converted_file.py' using the pyside-uic.exe converter.

Next convert your 'icons.qrc' file into a 'icons_rc.py' using the pyside-rcc.exe command

at the end of this process you should only need your icons folder with your .png icons your icons_rc.py file and your application script.

After that you can continue to use the Pyinstaller-2.0 as per the manual..

As I've said it is a partial answer because it works, however if someone else could tell me or explain to me how to make it work with my original .ico files then I suspect I would not have to go trough the whole process again

Upon more research this page was especially helpful http://qt-project.org/forums/viewthread/17342

so I went into my Qt4.8 folder and did a search for qico4.dll and made a copy of it in the folder where my script is located and the application ran perfectly, If I can find a way to include it inside my script then I think I will have a complete answer, the problem seems to be with Qt and not Pyinstaller, it seems that it needs to be aware of the dll in order to process .ico files



回答2:

I ran into the same problem as you. All the icons would seem to be missing when after pyinstaller created my executable (pyinstaller version 2.1 and PySide 1.2.1). However one of .png was displayed.

Therefore a possible workaround would be using .png instead of .ico for you icons in the PySide, and it will work on both sides (python and executable).

As an example:

QPushButton(QIcon("./icons/clear.png"),' &Clear',self.app_wgt)