Embedded Program Icon (wxPython)

B

Bart

Dear members,
It's not (yet) easy to find my way through the bunch of info scattered
over the internet to find out how to embed an icon. It could be that
I’m looking at the wrong places, I'm new to Python and wxPython.
Nevertheless, I would like to share this little script with the rest
of the world. Any suggestions are welcome!

Regards,
Bart


import wx
import cStringIO
import base64

def getImageStream():
img_b64 = \

"""iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABHNCSVQICAgIfAhkiAAAATlJREFUWIXNl2FyhCAMhV
+0B/NmCzfrHkxff+zSik0gCI77ZhwdCckHQoIi04w7Nd0a/
WMBuK0cHcjyKdYa4LZSplmanBXsrTYTQOvonZljHys4AHx5HWVgTBx5syjWpeBVgN+A75H/
BTbsmEBe9rXgQNcuqPp2qQqgj74cPJl61sxn5oGyxkx9B8BYZQDcVqbL7kLEGP691d55fGfb0LNtzsr0LdNcvQAQAPcKIZAESWTP5MsWAD2+b18DxVqwl5YPYowAgMcj7By
+787P2T0D+
+Bn5AZIIxKt4iSbxtE3AVylywFq9cBdjADf1B5tZJqlBFEEsE4zaR0sy4Ln8xsxBvUw4oEwAWpHqVZZEOap
+Iq0rPlUAa6sCS6AkrR8EELI2pr8nfk3bP0vGA5wBOn5ZF0AI3R7Kv4BLsO1PPzEcJMAAAAASUVORK5CYII="""
img = base64.b64decode(img_b64)
return img

class MyFrame(wx.Frame):
def __init__(self, parent, id, caption):
wx.Frame.__init__(self, parent, id, caption)

myStream = cStringIO.StringIO(getImageStream())
myImage = wx.ImageFromStream(myStream)
myBitmap = wx.BitmapFromImage(myImage)

myIcon = wx.EmptyIcon()
myIcon.CopyFromBitmap(myBitmap)
self.SetIcon(myIcon)

app = wx.PySimpleApp()
frame = MyFrame(None, -1, "Embedded Programm Icon Sample")
frame.Show(True)
app.MainLoop()
 
M

Mike Driscoll

Dear members,
It's not (yet) easy to find my way through the bunch of info scattered
over the internet to find out how to embed an icon. It could be that
I’m looking at the wrong places, I'm new to Python and wxPython.
Nevertheless, I would like to share this little script with the rest
of the world. Any suggestions are welcome!

Regards,
Bart

import wx
import cStringIO
import base64

def getImageStream():
    img_b64 = \

"""iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABHNCSVQICAgIfAhkiAAAATlJREFUWIXNl2FyhCAMhV
+0B/NmCzfrHkxff+zSik0gCI77ZhwdCckHQoIi04w7Nd0a/
WMBuK0cHcjyKdYa4LZSplmanBXsrTYTQOvonZljHys4AHx5HWVgTBx5syjWpeBVgN+A75H/
BTbsmEBe9rXgQNcuqPp2qQqgj74cPJl61sxn5oGyxkx9B8BYZQDcVqbL7kLEGP691d55fGfb0LNtzsr0LdNcvQAQAPcKIZAESWTP5MsWAD2+b18DxVqwl5YPYowAgMcj7By
+787P2T0D+
+Bn5AZIIxKt4iSbxtE3AVylywFq9cBdjADf1B5tZJqlBFEEsE4zaR0sy4Ln8xsxBvUw4oEwAWpHqVZZEOap
+Iq0rPlUAa6sCS6AkrR8EELI2pr8nfk3bP0vGA5wBOn5ZF0AI3R7Kv4BLsO1PPzEcJMAAAAASUVORK5CYII="""
    img = base64.b64decode(img_b64)
    return img

class MyFrame(wx.Frame):
    def __init__(self, parent, id, caption):
        wx.Frame.__init__(self, parent, id, caption)

        myStream = cStringIO.StringIO(getImageStream())
        myImage = wx.ImageFromStream(myStream)
        myBitmap = wx.BitmapFromImage(myImage)

        myIcon = wx.EmptyIcon()
        myIcon.CopyFromBitmap(myBitmap)
        self.SetIcon(myIcon)

app = wx.PySimpleApp()
frame = MyFrame(None, -1, "Embedded Programm Icon Sample")
frame.Show(True)
app.MainLoop()

wxPython provides the img2py utility for this sort of thing. On my
Windows box, it was here:

L:\Python25\Lib\site-packages\wx-2.8-msw-unicode\wx\tools

You can read up on how to use it here:

http://www.blog.pythonlibrary.org/?p=34

Unfortunately, that article is a little dated. The newest version of
wxPython includes an updated img2py utility that I'm told supports the
previous API, but I haven't tested it. One of these days, I'll have to
update that post to reflect the new way though...

Glad you found an alternate way to do it though. I recommend
subscribing to the wxPython group. They can give you additional
pointers, should you need them: http://wxpython.org/maillist.php

Mike
 
B

Bart

wxPython provides the img2py utility for this sort of thing. On my
Windows box, it was here:

L:\Python25\Lib\site-packages\wx-2.8-msw-unicode\wx\tools

You can read up on how to use it here:

http://www.blog.pythonlibrary.org/?p=34

Unfortunately, that article is a little dated. The newest version of
wxPython includes an updated img2py utility that I'm told supports the
previous API, but I haven't tested it. One of these days, I'll have to
update that post to reflect the new way though...

Glad you found an alternate way to do it though. I recommend
subscribing to the wxPython group. They can give you additional
pointers, should you need them:http://wxpython.org/maillist.php

Mike

Thanks a lot Mike!

In Dutch we would say 'handig'! (Translated as 'handy')
Your fine example was what I was looking for the whole day :)

I tried this thing for my self but my 'img2py.py' gives indeed an
other script (see below). To make this new version of 'myIcon.py' work
with your sample script 'embed_icons3.py', you've to change the last
line of 'myIcon.py':

getpyIcon = py.GetIcon

,into

getIcon = py.GetIcon

That piece of code in my previous post was a collection of code
snippets I found in all different kind of examples. Your way, I think,
is much more elegant. Actually, simple.

Regards,

Bart



Script of myIcon.py generated with the new version of img2py.py
(Version: wxPython 2.8.9.1 for Python 2.5)

# myIcon.py
#
#----------------------------------------------------------------------
# This file was generated by C:\Python25\Lib\site-packages\wx-2.8-msw-
unicode\wx\tools\img2py.py
#
from wx.lib.embeddedimage import PyEmbeddedImage

py = PyEmbeddedImage(

"iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABHNCSVQICAgIfAhkiAAAAvxJ"
"REFUWIXtl0tPU0EUgL+5baHQlncpBKkPMGpYIKiJKw1xoysXGk1cGLf+Bv
+CS6Mr3ZoYNHFh"
"ohuMcYWYBowkClYaCG0pD9vS0td9uJhCH/TSFhaYyEkm9845nXO
+mXt6ZkYIxcJhinKo0QEB"

"jwww6hzmRyjjolhj6LcNGKgbwAo2GHXDqAcsovoIgMV2+DBeqrtxEbyn5Hs0Db4IzMdkv8kC"
"Fzww1Cn7mgG+FfBFsAJwqQceDkNjjfnwqRMj9NhgOiT753vh5ghcPS77/
igk1QKAwwZj/XDv"

"rOxnNHg2UwRgt0Jro3zWIuc8cGcEjnvgaxCsdnDYoc0u7a4GaCiajCKgyVqwp9WdWDVGLJOB"
"djjdA4NdsJqErLovN+YAPzdgdg1SWqm
+3wlDXeB2gF0BpwWsArL7jm8C8G0VXnyH9XSpfqwf"
"upuhww6plGyaVtHFwQASOQgmIZIq1a+lIKeDEGAYoOsHCg7/
QCGqvAJeF1zzQixTqh/thpaG"
"6l4dNhh2y2wH+Q/zuuoAGHZDrxPUsiVuaQB3c3WADjtcPwGXe/
NRFOhuqgOgq1k2M1ndgkBc"
"VrONNOQMmI6A0wYnW8HjAK8NvGXjshoEYrJQLcT2AKgmgRi8moMvYQglQAfe+mFxE
+6ekQCV"
"JKvB52V4Mw
+h5B4AwU1Y2oSsSZb7IjL4XBCIAgb8apOfzOsCuwWOuQogGVX680dhMgS+cH5c"
"zARgZg1ez0E0U9HMn3R+BlGE8lwAGPoDg9VWeLcASwm4NVgASObg41LetgkYCOWpMF
+BcBKm"
"ViASAeKAWbEJFr0vQtIKP1rkbnelr2DK6fA7BpPLeX/
RHVOVHFhBKC9r2qOFMiFgAkO/b0Cf"
"ya9UhPKkxF+VQhSvJXaZJPew7c6pQ6+ERwBHAEcAshClVbn3b+/
fKRX0ei8rRaLpsJWT9wOA"
"eEYexU0BpsKyfG5fTGbXZf3er2yk4X0A/Pl7wVZOnjMrA
+TAtyzb7qnsI7oG0QRMJGCi3Lb7"
"+C7++9vxX9DXCWOvj7llAAAAAElFTkSuQmCC")
getpyData = py.GetData
getpyImage = py.GetImage
getpyBitmap = py.GetBitmap
getIcon = py.GetIcon
 

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,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top