简体   繁体   English

在Mac OS X中设置Tkinter Python应用程序图标

[英]Set Tkinter Python application icon in Mac OS X

I have created a Tkinter GUI in Python. 我用Python创建了一个Tkinter GUI。 The following snippet shows how when the application starts it changes its icon to the one in icon.gif. 以下代码片段显示了应用程序启动时如何将其图标更改为icon.gif中的图标。 This works on Ubuntu and Windows however it does not appear to do anything in Mac. 这可以在Ubuntu和Windows上运行,但是在Mac中似乎没有任何作用。 If it helps I'm running Python 2.7.10 on OS X 10.9.5 with Tcl 8.5 & Tk 8.5 (8.5.18). 如果有帮助,我将在OS X 10.9.5和Tcl 8.5&Tk 8.5(8.5.18)上运行Python 2.7.10。 How can I change the icon that appears in the dock? 如何更改出现在扩展坞中的图标?

import Tkinter as TK

class MyClass(object):
    def __init__(self, root):
        pass

if __name__ == '__main__': 
        root = TK.Tk()
        my_app = MyApp(root)
        icon_path = 'some\long\path\to\icon.gif'
        img = TK.PhotoImage(file=icon_path)
        root.tk.call('wm', 'iconphoto', root._w, img)
        root.mainloop()
        root.destroy()

I know this is old, but i just saw the answer before coming here and I thought I'd share the answer in case someone stumbles on this in the future and wants to know. 我知道这很古老,但是在来到这里之前我只是看到了答案,我想我会分享答案,以防将来有人偶然发现并想知道。

You must supply a icon file in the .icns format in order for your app to have an application icon. 您必须提供.icns格式的图标文件,才能使您的应用程序具有应用程序图标。 py2app has three ways of doing this: py2app具有三种执行此操作的方法:

  • command line argument 命令行参数
  • options dictionary in setup.py setup.py中的options字典
  • setup.cfg file setup.cfg文件

In all following situations, replace app.icns with the full path to your desired .icns file 在以下所有情况下,将app.icns替换为所需.icns文件的完整路径。

CLI argument CLI参数

$ python setup.py py2app --iconfile app.icns

Setup.py dictionary Setup.py字典

from setuptools import setup
APP = ['YourApp.py']
APP_NAME = "YourApp"
DATA_FILES = []


OPTIONS = {
    'argv_emulation': True,
    'iconfile': 'app.icns',
}

setup(
    name=APP_NAME,
    app=APP,
    data_files=DATA_FILES,
    options={'py2app': OPTIONS},
    setup_requires=['py2app'],
)

Setup.cfg Setup.cfg

I'm honestly not sure about this one, I don't touch .cfg files 老实说,我不确定这一点,我不触摸.cfg文件

[py2app]
iconfile="app.icns"

Here is the relevant relevant documentation . 这是相关的相关文档 A practical example is available as well. 一个实际的例子也是可用的。

If you mean the icon in the top left corner this might not work but to create an app for Mac with an icon you do this: 如果您的意思是左上角的图标可能不起作用,但是要为Mac创建带有图标的应用程序,请执行以下操作:

To add an icon you have to make a .icns file. 要添加图标,您必须制作一个.icns文件。 Follow these or these instructions. 请遵循这些这些说明。 Secondly you have to make the program into an app. 其次,您必须将程序制作成应用程序。 To do this follow the instructions here but don't do it yet. 为此,请按照此处的说明进行操作但请不要这样做。 Ok now go into setup.py and add under options 'iconfile' : 'icon.icns' . 现在,进入setup.py并在选项'iconfile' : 'icon.icns'下添加'iconfile' : 'icon.icns' Create the app now. 立即创建应用。 It will only work if you put the .icns image in the same folder as setup.py. 仅当您将.icns图像放置在setup.py所在的文件夹中时,它才有效。 This will add an icon to the app. 这将在应用程序中添加一个图标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM