Which file formats can I use for tkinter icons?

后端 未结 2 1217
青春惊慌失措
青春惊慌失措 2021-01-06 16:52

I know this might be obvious, but in tkinter you can set an icon, but I have found it really hard to find one. I just wanted to know if you have to use the .ico

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-06 17:34

    I was struggling a lot to find an answer too but finally I peeked into the source code of idle3.6 where I found the following piece of code:

    # set application icon
    icondir = os.path.join(os.path.dirname(__file__), 'Icons')
    if system() == 'Windows':
        iconfile = os.path.join(icondir, 'idle.ico')
        root.wm_iconbitmap(default=iconfile)
    else:
        ext = '.png' if TkVersion >= 8.6 else '.gif'
        iconfiles = [os.path.join(icondir, 'idle_%d%s' % (size, ext))
                     for size in (16, 32, 48)]
        icons = [PhotoImage(master=root, file=iconfile)
                 for iconfile in iconfiles]
        root.wm_iconphoto(True, *icons)
    

    I searched through all files in the idlelib folder for .ico and .png using the rummage software.

    So finally I managed to get the window icon working (on GNU-linux with TkVersion>=8.6) with the following two lines:

    icon = PhotoImage(master=root, file='icon.png')
    root.wm_iconphoto(True, icon)
    

    where I put the icon directly in my application folder.

    From the idle code it seems to me that on Windows still only .ico files are supported.

提交回复
热议问题