10

I have a small program in PyQt4 and I want to compile the program into an Exe. I am using py2exe to do that. I can successfully set icon in the windows title bar using the following code, but when i compile it into exe the icon is lost and i see the default windows application. here is my program:

import sys
from PyQt4 import QtGui


class Icon(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Icon')
        self.setWindowIcon(QtGui.QIcon('c:/python26_/repy26/icons/iqor1.ico'))


app = QtGui.QApplication(sys.argv)
icon = Icon()
icon.show()
sys.exit(app.exec_())

**** Here is the setup.py for py2exe****

from distutils.core import setup
import py2exe

setup(windows=[{"script":"iconqt.py"
               ,"icon_resources": [(1, "Iqor1.ico")]}]
                   ,options={"py2exe":{"includes":["sip", "PyQt4.QtCore"]}})
Caleb Huitt - cjhuitt
  • 14,785
  • 3
  • 42
  • 49
realz
  • 123
  • 1
  • 6

4 Answers4

15

The problem is that py2exe doesn't include the qt icon reader plugin. You need to tell it to include it with the data_files parameter. Something along these lines:

setup(windows=[{"script":script_path,
                "icon_resources":[(1, icon_path)]}], 
      data_files = [
            ('imageformats', [
              r'C:\Python26\Lib\site-packages\PyQt4\plugins\imageformats\qico4.dll'
              ])],
      options={"py2exe":{"packages":["gzip"], 
                         "includes":["sip"]}})
Jesse Aldridge
  • 7,991
  • 9
  • 48
  • 75
  • 3
    Just perfect. Exactly solve my problem. Ps this trick works for PySide too. – Pinch Mar 24 '12 at 14:15
  • I have a similar setup, but it's not working. Would you mind taking a look at it? http://stackoverflow.com/questions/17687347/win7-taskbar-icon-incorrect-for-pyside-py2exe-app – ArtOfWarfare Jul 17 '13 at 12:15
  • 1
    What would you have to change to make this work with options -> bundle_files set to 1? – ArtOfWarfare Jul 17 '13 at 12:46
3

I believe you need to reference the .ico file directly from the EXE or DLL that you are creating with py2exe. You seem to have the setup.py script correct, so take a look at: http://www.py2exe.org/index.cgi/CustomIcons. There is an example for wxWidgets, but you could try to adapt it to Qt.

swanson
  • 7,377
  • 4
  • 32
  • 34
0

I would suggest you to create a file called YourApp.rc, add up the following line :

IDI_ICON1   ICON    DISCARDABLE "res/icons/app_icon.ico"

Then in your .PRO file, add up the following lines :

win32{
RC_FILE = YourApp.rc
}

It should fix your problem !

Andy M
  • 5,945
  • 7
  • 51
  • 96
  • What .PRO file? Py2exe uses setup.py. This answer doesn't make sense. – James Nov 12 '10 at 19:10
  • 1
    Yeah, my bad, I thought they used the same process as it is in C++. The last part of your comment is unnecessary. – Andy M Nov 13 '10 at 09:35
0

I had the same issue. For some reason it worked just fine with a image.png file & not an image.ico file. No clue why. But i converted the ico to png & it worked