简体   繁体   中英

Set Tkinter Python application icon in Mac OS X

I have created a Tkinter GUI in Python. The following snippet shows how when the application starts it changes its icon to the one in icon.gif. This works on Ubuntu and Windows however it does not appear to do anything in 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). 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. py2app has three ways of doing this:

  • command line argument
  • options dictionary in setup.py
  • setup.cfg file

In all following situations, replace app.icns with the full path to your desired .icns file

CLI argument

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

Setup.py dictionary

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

I'm honestly not sure about this one, I don't touch .cfg files

[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:

To add an icon you have to make a .icns file. 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' . Create the app now. It will only work if you put the .icns image in the same folder as setup.py. This will add an icon to the app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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