Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   Set application Icon wxPython (http://www.programmingforums.org/showthread.php?t=15370)

OpenLoop Mar 8th, 2008 10:42 AM

Set application Icon wxPython
 
Anyone knows how to set the application icon for a wxPython app? I have some leads about the wx.Frame.SetIcon() function, but how do I load the icon from a .ico or .bmp file into my program?

Sane Mar 8th, 2008 12:31 PM

Re: Set application Icon wxPython
 
There's a wxBitmap function. Check the alphabetical class reference in the docs. I'll look for the exact code when I get home.

Sane Mar 8th, 2008 1:57 PM

Re: Set application Icon wxPython
 
I believe there are multiple ways to do it, including the wxBitmap way. But the easiest way is with wxIcon.

Here, wx.BITMAP_TYPE_ICO specifies that the file is .ico. I've found that no other format will handle transparency very well (including PNG) for application icons.

:

class myApp(wx.Frame):

    def __init__(self, parent):

        wx.Frame.__init__(self, None,
            pos=wx.DefaultPosition, size=wx.Size(548, 438),
            style=wx.MINIMIZE_BOX | wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.CLIP_CHILDREN,
            title="My Application")

        favicon = wx.Icon('favicon.ico', wx.BITMAP_TYPE_ICO, 16, 16)
        wx.Frame.SetIcon(self, favicon)


Edit: And here's the Alphabetical Class Reference. Perhaps the most useful thing out there for wxWidgets and wxPython.

OpenLoop Mar 8th, 2008 2:30 PM

Re: Set application Icon wxPython
 
Works great. I was going about it the wrong way trying to self.SetIcon() on on the frame class since I couldn't find decent references for this specific issue under wxPython documentations. I really miss the F1 in C# & MSDN when working with python. Thanks for the help.

Game_Ender Mar 9th, 2008 5:10 PM

Re: Set application Icon wxPython
 
I am pretty sure those last two lines can be written like this as well (notice the self.SetIcon()):
:

  1.         favicon = wx.Icon('favicon.ico', wx.BITMAP_TYPE_ICO, 16, 16)
  2.         self.SetIcon(favicon)

All that other code was doing was invoking an unbound member function and passing it the object to work with.

OpenLoop Mar 9th, 2008 5:46 PM

Re: Set application Icon wxPython
 
I tried that and it seems to work... I was calling self.SetIcon(self, favicon) and it complained about the arguments but my real issue was reading the icon from a file.

I really don't understand the 'self' keyword. I try to think of it as 'this' in C++, but why does every function of a class has to have it as first argument in the definition, but then when you call it you have to omit the first argument? It's just a confusing way to do things!

Back to the icon issue, this is unrelated but if anyone out there is looking for a way to set the icon of an exe file when using py2exe, here's how your setup.py should look like:
:

  1. from distutils.core import setup
  2. import py2exe
  3.  
  4. setup(
  5.     windows = [
  6.         {
  7.                 "script": "Datetool.pyw",
  8.             "icon_resources": [(1, "icon.ico")]
  9.         } 
  10.     ],
  11. )



All times are GMT -5. The time now is 12:53 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC