How to embed icon file for wxPython app?

869 views
Skip to first unread message

wryb...@gmail.com

unread,
Nov 8, 2007, 7:15:37 PM11/8/07
to PyInstaller
I know how to embed an icon for the single file executable, but I
can't figure out how to do it for wxPython apps. For example, my app
gets the icon in the top left corner of the window like this:

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

This works great if the icon.ico file is a separate file. But it won't
work if I don't have the file.

Is there a way to embed it into the single file exe?

Hannes Müller

unread,
Nov 9, 2007, 5:40:14 AM11/9/07
to PyIns...@googlegroups.com
We use the following:

self.SetIcon(Resources.getIcon())

and in our Resources module are the functions generated by the following code:

"""This module contains a simple function to encode any number of
bitmap files to a .py file"""

import os
import glob

from wx.tools import img2py

output = 'bitmaps.py'

# get the list of BMP files
files = glob.glob('*.png') #TODO: chose your extension here

open(output, 'w')

# call img2py on each file
for file in files:

# extract the basename to be used as the image name
name = os.path.splitext(os.path.basename(file))[0]

# encode it
if file == files[0]:
cmd = "-u -i -n %s %s %s" % (name, file, output)
else:
cmd = "-a -u -i -n %s %s %s" % (name, file, output)
img2py.main(cmd.split())

Hope that gets you going,

Hannes

Giovanni Bajo

unread,
Nov 9, 2007, 5:37:43 PM11/9/07
to PyIns...@googlegroups.com

I don't know wxPython, but surely there's no portable way to extract the
icon from the Windows executable. What PyInstaller can do is to embed
the icon into the executable (in Windows, and one day in Mac), but
accessing the icon itself is not something that can be easily done.

You're probably better off adding the icon to your resources, as Hannes
suggested you.
--
Giovanni Bajo

wryb...@gmail.com

unread,
Dec 9, 2007, 5:33:36 AM12/9/07
to PyInstaller
I found a great way to do this when using WX. It uses the img2py.py,
which is packaged with WX Python. It reads your icon into a string
which is then importable to your script before the PyInstaller stage.
Very easy and very neat and clean.

Here's my notes on it:

1) navigate to the WX tool directory, on my installation that's C:
\Python24\Lib\site-packages\wx-2.8-msw-unicode\wx\tools

2) run "img2py.py -i pycon.ico iconfile.py"

3) copy the iconfile.py to your project's directory

4) put these lines in your app:

import iconfile
myicon = iconfile.getIcon()
self.SetIcon(myicon)

Handy dandy!


Reply all
Reply to author
Forward
0 new messages