5

I am trying to set an icon in my wxpython program. So far, after reading many pages and examples, I was able to set an icon at the window, which also works when using alt+tab (I'm working over Windows 7).

But the icon at task bar is the usual python default icon.

I don't understand why are there so many troubles for such a simple task.

Here is my code:

class GraphFrame(wx.Frame):
    """ The main frame of the application
    """
    title = 'My first wxprogram'

    def __init__(self):
        wx.Frame.__init__(self, None, -1, self.title)

        ico = wx.Icon('dog.ico', wx.BITMAP_TYPE_ICO)
        self.SetIcon(ico)
        self.set_icon  

        self.create_menu()
        self.create_status_bar()
        self.create_main_panel()
        #...
Roman Rdgz
  • 12,836
  • 41
  • 131
  • 207

4 Answers4

11

I have found a fix to the problem described in my second answer in another SO question/answer which is PyQt related. Add this code to your application before the GUI is created:

import ctypes
myappid = 'mycompany.myproduct.subproduct.version' # arbitrary string
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)

The icon will be set correctly with either taskbar buttons settings.

Explanation can be found here: https://stackoverflow.com/a/1552105/674475

Community
  • 1
  • 1
Fenikso
  • 9,251
  • 5
  • 44
  • 72
3

It's currently not possible to set the taskbar icon via wxPython (Unless you hack apart the system variables), this is because windows gets the application icon from the executable (Which in your case is Python)

If you use either pyinstaller or py2exe (I prefer the former), when compiling it can set the applications icon - which will make the taskbar icon correct.

If using pyinstaller, you'll want to set the icon as such in the specfile:

exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.datas,
          name=os.path.join('..\\path\\to\\output', 'AppName.exe'),
          icon='C:\\abs\\path\\to\\icon\\icon.ico',
          debug=False,
          strip=False,
          upx=False,
          console=False )

The icon=... line sets the taskbar icon.

The rest of your python code is fine as is.

TyrantWave
  • 4,583
  • 2
  • 22
  • 25
3

It should work as expected. I guess your problem is just the icon format. I am usually embedding icons in the code using img2py.py script included with wxPython. Here is a working example:

enter image description here

import wx
from wx.lib.embeddedimage import PyEmbeddedImage

plane = PyEmbeddedImage(
    "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABHNCSVQICAgIfAh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+Q6QYssPOfX/t+gTnvCEx4pfALyBxD1SVtncAAAAAElFTkSuQmCC")

class MainWindow(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)
        self.SetIcon(plane.GetIcon())
        self.panel = wx.Panel(self)
        self.Show()

app = wx.App(False)
win = MainWindow(None)
app.MainLoop()
Fenikso
  • 9,251
  • 5
  • 44
  • 72
  • What version of Python and Windows? Running your code, the icon's not set - uses the Python app icon. (Windows x64, python 2.7.3). Also checked on a x86 computer, and the same behaviour - icon not set. (Same python and wx) – TyrantWave Mar 06 '13 at 09:55
  • @TyrantWave - Interesting! I have never had any problems with this. Now I am starting to get curious. Maybe a bug in wxPython? Taskbar or windows effect settings? OS: Windows 7 32-bit, Python: 2.7.3, wxPython: 2.8.12.1. – Fenikso Mar 06 '13 at 10:07
  • Are you using Aero or not maybe? Perhaps non-Aero can override the icon, but with it enabled windows only takes the icon from the executable? I'm running the same python/wx, and both x86/x64 windows. – TyrantWave Mar 06 '13 at 10:09
  • But I have most of the effects disabled, for example disabled Aero Peek. – Fenikso Mar 06 '13 at 10:12
  • Hm, then I'm not sure - I just tested different environments (python/pythonw, from an IPython shell, from eclipse, and from an admin command prompt), and they all don't set the icon. – TyrantWave Mar 06 '13 at 10:16
  • Interesting. I have tried python/pythonw, run from Eclipse, enable all effects, disable all effect. Always sets the icon. What does `platform.platform()` return in your environment? I get `Windows-7-6.1.7600`. Do you have all Windows updates installed? – Fenikso Mar 06 '13 at 11:03
  • 1
    Aha! I have found it! "Taskbar and Start Menu Properties / Taskbar / Taskbar buttons / Always combine and **Hide labels**". With labels shown it works. – Fenikso Mar 06 '13 at 11:06
  • Ah, that does indeed work. I hide the labels all the time (As an icon does more to explain it than any text) – TyrantWave Mar 06 '13 at 11:17
3

Both TyrantWave's and mine answers are true under specific circumstances. It depends on Windows 7 settings:

Taskbar buttons settings

When labels are hidden, the icon does not seem to be set:

enter image description here

With labels shown it works just fine as expected:

enter image description here

Fenikso
  • 9,251
  • 5
  • 44
  • 72