任务栏的wxpython图标

时间:2013-03-05 12:34:13

标签: python icons wxpython

我正在尝试在我的wxpython程序中设置一个图标。到目前为止,在阅读了许多页面和示例之后,我能够在窗口设置一个图标,这在使用alt + tab时也有效(我在Windows 7上工作)。

但任务栏上的图标是通常的python默认图标。

我不明白为什么这么简单的任务会有这么多麻烦。

这是我的代码:

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()
        #...

4 个答案:

答案 0 :(得分:8)

我找到了另一个SO问题/答案与PyQt相关的第二个答案中描述的问题。在创建GUI之前将此代码添加到您的应用程序:

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

使用任务栏按钮设置可以正确设置图标。

可在此处找到解释:https://stackoverflow.com/a/1552105/674475

答案 1 :(得分:3)

它应该按预期工作。我想你的问题只是图标格式。我通常使用wxPython附带的img2py.py脚本在代码中嵌入图标。这是一个有效的例子:

enter image description here

import wx
from wx.lib.embeddedimage import PyEmbeddedImage

plane = PyEmbeddedImage(
    "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABHNCSVQICAgIfAhkiAAABgVJ"
    "REFUWIXtknlQ1GUYx7+/3WWBXWm5F4RFLh1mNZR1UYMBw1tMCM3RUNHRxswuxtIoGGeiqEkp"
    "J/PWvEJFvIIUFfBsN664N3OJ5VgC2WVPYJe93/5oKrM4KpvxDz///d739zzf7/N9H+AJjyNG"
    "ty2+j6KPiEr0Sc6VRI66IKvQxMu+pb4peL9Q9fLRtpf+i7iTE4Na/lllZeBOiWa4/2gPfrQ2"
    "99rl0kb+2g0LvCoHWg+d0/J3n8imXP6NgdwbmFeoHjOt1z/UY9QGTm3hdeu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    "AbRYGChpMeDM+WrERUdfEV2pyBjr5uA5sW0nZy+exokI9YSK4YLWfjP6TFYwnJnQaAag/6lL"
    "NSjTfmgO/npv1fp91iEN9EzevHRXbvq2YDdTZGIEF2OIDf12gg4TUG1i4sy5RrS3dSsTowSZ"
    "RzaGHwaAD64ajn4iuZeGp8NoLjeqsPgpLYmKj6KcArnoIXT0GC2w0ujolnWDa3etfi5yfsay"
    "UOmNP03NeJFOL8P0PZlFNUSq0hG9TkN6VCrSpFCTMy0aklrURjyWHiFvi+l5OuqPODdu13GC"
    "U3P6w3OLCdacInGbi+9knYdQxnl++ck6Y2lOYbVtr+Q+yZUbyFstAyS1XkXij4odxUS4n8Xb"
    "HPB7AuyUj7Ky05M/WCcIArGYoHZQkPUTiA0MfLVPBLoLTTrV3z+9YIvg6sNPVsGMX7l11apX"
    "pvAjj88MiTmydKnD/tsdc/6ByZp7V1Z/sSt7uYuHc6Azzw+9TFc0tith7lTf/+l2zS7kS72F"
    "7HnFhk9F/aRTrSLlXWqyv62PJBxoIKxF+6zXGAk7tr97nTPUEo2GOx6fu5eoI1aerjeVZJ+v"
    "cByUKknGXQ2ZmVvWQW0t+rGBaXCPbGK54tloBm7fkONmnhiClNhvSZP0nVu7U8r/i/jDnJBG"
    "Tp3zjOfa76vLeYVXWjZRM7IvWJJSpjvdkunR8L3MFukXIor2GXvikxbf4/ZMm+NRiv8dVIl9"
    "fgLNk71QGMHX+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()

答案 2 :(得分:3)

在特定情况下,TyrantWave和我的答案都是正确的。这取决于Windows 7设置:

Taskbar buttons settings

隐藏标签时,似乎未设置图标:

enter image description here

显示标签后,它可以正常工作:

enter image description here

答案 3 :(得分:2)

目前无法通过wxPython设置任务栏图标(除非你破坏了系统变量),这是因为windows从可执行文件中获取应用程序图标(在你的情况下是Python)

如果您使用pyinstallerpy2exe(我更喜欢前者),则在编译时可以设置应用程序图标 - 这将使任务栏图标正确。

如果使用pyinstaller,您需要在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 )

icon=...行设置任务栏图标。

你的python代码的其余部分都没问题。