setting icon using py2exe?

G

Grant Edwards

I'm trying in vain to set the icon for the executable generated
by py2exe. According to various sources there are two answers:

1) Do it on the command line:

python setup.py py2exe --icon foo.ico

That generates a usage error:

error: --icon not recognized

2) According to http://starship.python.net/crew/theller/moin.cgi/CustomIcons
you can set the icon_resources in the setup.py like this:

# setup.py
from distutils.core import setup
import py2exe
setup(windows=[{"script":"vfcupdate.py","icon_resources":[(1,"rivatek.ico")]}])

That doesn't work either:

running py2exe
[...]
copying C:\Python23\Lib\site-packages\wx\wxmsw251h_html_vc.dll -> C:\cygwin\home\admin\othertools\dist
copying C:\Python23\Lib\site-packages\wx\wxbase251h_vc.dll -> C:\cygwin\home\admin\othertools\dist
copying C:\PYTHON23\w9xpopen.exe -> C:\cygwin\home\admin\othertools\dist
copying C:\WINDOWS\SYSTEM\python23.dll -> C:\cygwin\home\admin\othertools\dist
copying C:\Python23\Lib\site-packages\py2exe\run_w.exe -> C:\cygwin\home\admin\othertools\dist\vfcupdate.exe
Traceback (most recent call last):
File "setup.py", line 5, in ?
setup(windows=[{"script":"vfcupdate.py","icon_resources":[(1,"rivatek.ico")]}])
File "C:\PYTHON23\lib\distutils\core.py", line 149, in setup
dist.run_commands()
File "C:\PYTHON23\lib\distutils\dist.py", line 907, in run_commands
self.run_command(cmd)
File "C:\PYTHON23\lib\distutils\dist.py", line 927, in run_command
cmd_obj.run()
File "C:\Python23\Lib\site-packages\py2exe\build_exe.py", line 197, in run
self.create_binaries(py_files, extensions, dlls)
File "C:\Python23\Lib\site-packages\py2exe\build_exe.py", line 395, in create_binaries
arcname, target.script)
File "C:\Python23\Lib\site-packages\py2exe\build_exe.py", line 577, in build_executable
add_icon(unicode(exe_path), unicode(ico_filename), ico_id)
RuntimeError: MapExistingFile: The handle is invalid.


Has anybody really been able to set the icon for the executable
generated by py2exe?
 
G

Grant Edwards

2) According to http://starship.python.net/crew/theller/moin.cgi/CustomIcons
you can set the icon_resources in the setup.py like this:

# setup.py
from distutils.core import setup
import py2exe
setup(windows=[{"script":"vfcupdate.py","icon_resources":[(1,"rivatek.ico")]}])

That doesn't work either:
File "C:\Python23\Lib\site-packages\py2exe\build_exe.py", line 577, in build_executable
add_icon(unicode(exe_path), unicode(ico_filename), ico_id)
RuntimeError: MapExistingFile: The handle is invalid.

Never mind....

Further googling reveals this is a known bug when running under
Win98/Me:

http://groups.google.com/groups?th=4d594c535345b98b
 
C

Chris Liechti

I'm trying in vain to set the icon for the executable generated
by py2exe. According to various sources there are two answers:

1) Do it on the command line:

python setup.py py2exe --icon foo.ico

That generates a usage error:

error: --icon not recognized

that is for versions < 0.4
it is not longer supported in 0.5+
2) According to
http://starship.python.net/crew/theller/moin.cgi/CustomIcons
you can set the icon_resources in the setup.py like this:

# setup.py
from distutils.core import setup
import py2exe
setup(windows=[{"script":"vfcupdate.py","icon_resources":[(1,"riv
atek.ico")]}]) ....
RuntimeError: MapExistingFile: The handle is invalid.

Has anybody really been able to set the icon for the executable
generated by py2exe?

yes, works fine here:
windows = [
{ 'script': "hexedit_wx.py", 'icon_resources': [(0x0004,
'bigicon.ico')]},
],

i was told that the ID does not matter that much, it will just take the
first icon resource. i somewhere saw the use of 4, so i tested with that
number and since it worked for me, i didn't change it

chris
 
S

simo

I think you have to have win32all/pywin32 installed to do
resource/icon handling.

This works fine for me on Windows 98SE/XP/2000:

setup(
windows = [
{
"script": "hellow.py",
"icon_resources": [(1, "hellow.ico")]
}
],
)

If you can't get this to work, you could just include the .ico file
using data_files=["hellow.ico"], and then create the shortcuts,
pointing to the icon, using your install program, e.g. InnoSetup
(kinda like I do under Linux).
 
T

Thomas Heller

Grant Edwards said:
2) According to http://starship.python.net/crew/theller/moin.cgi/CustomIcons
you can set the icon_resources in the setup.py like this:

# setup.py
from distutils.core import setup
import py2exe
setup(windows=[{"script":"vfcupdate.py","icon_resources":[(1,"rivatek.ico")]}])

That doesn't work either:
File "C:\Python23\Lib\site-packages\py2exe\build_exe.py", line 577, in build_executable
add_icon(unicode(exe_path), unicode(ico_filename), ico_id)
RuntimeError: MapExistingFile: The handle is invalid.

Never mind....

Further googling reveals this is a known bug when running under
Win98/Me:

http://groups.google.com/groups?th=4d594c535345b98b

The bug should be fixed in CVS. Since py2exe now can also be built with
MingW32, you could try that, even if you don't have MSVC. Or you have
to wait for the release.

Thomas
 
G

Grant Edwards

I think you have to have win32all/pywin32 installed to do
resource/icon handling.

I do.
This works fine for me on Windows 98SE/XP/2000:

Are you sure it works on 98SE? Others have reported it only
works on NT/2K/XP.
setup(
windows = [
{
"script": "hellow.py",
"icon_resources": [(1, "hellow.ico")]
}
],
)

If you can't get this to work, you could just include the .ico file
using data_files=["hellow.ico"], and then create the shortcuts,
pointing to the icon, using your install program, e.g. InnoSetup
(kinda like I do under Linux).

I've done that. The desktop icon and start menu icon are set
by InnoSetup, but the icon doesn't show up in the window banner
or in the task bar. I was assuming that setting the icon
resource in the executable would fix that.
 
G

Grant Edwards

The bug should be fixed in CVS. Since py2exe now can also be built with
MingW32, you could try that, even if you don't have MSVC. Or you have
to wait for the release.

Thanks! If I run out of things to do, I'll install MingW32 and
try to build the CVS code. Otherwise I'll wait for the next
release.
 
G

Grant Edwards

The correct icons show up in the desktop explorer (which is
new), but I still don't get my icons showing up in the window's
banner or in the taskbar when the program is running. :/

I guess you have to set that at runtime. I had thought that if
the .exe file had icon resources, Windows would use them by
default, but I guess not. [In wxPython you call the SetIcon()
method of the top-level frame.]
 
S

simo

[snip]
Are you sure it works on 98SE? Others have reported it only
works on NT/2K/XP.

Well I don't know about *building* in 98SE (I only use 2000/XP for
development) but the resulting binary certainly incorporates the icon
in 98SE/NT4/2000/XP.

[...]
If you can't get this to work, you could just include the .ico file
using data_files=["hellow.ico"], and then create the shortcuts,
pointing to the icon, using your install program, e.g. InnoSetup
(kinda like I do under Linux).
I've done that. The desktop icon and start menu icon are set
by InnoSetup, but the icon doesn't show up in the window banner
or in the task bar. I was assuming that setting the icon
resource in the executable would fix that.

No, you have to set the icon from wxPython for the TaskBar and window
icon, py2exe only fixes the *executable's* icon (in Explorer).

In your mainWindow(wx.Frame) do:

self.icon = wx.Icon("hellow.ico", wx.BITMAP_TYPE_ICO)
self.SetIcon(self.icon)

This works on Linux too (for window and Panel), which I was quite
pleased with, especially as I didn't even do an XPM version, just the
ICO file....
 
G

Grant Edwards

No, you have to set the icon from wxPython for the TaskBar and
window icon, py2exe only fixes the *executable's* icon (in
Explorer).

It seems odd that by default Windows won't use the executable's
icon for its window. But, a lot of things in Windows seem odd
to me...
In your mainWindow(wx.Frame) do:

self.icon = wx.Icon("hellow.ico", wx.BITMAP_TYPE_ICO)
self.SetIcon(self.icon)

Yup, that does it.
This works on Linux too (for window and Panel), which I was
quite pleased with, especially as I didn't even do an XPM
version, just the ICO file....

That's cool! It didn't even occur to me to try to use the .ico
file under Linux.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top