Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set tkinter icon on Mac OS

I am trying to change the icon that appears on my tk application for Mac OS. The last time I checked this code worked for windows. The goal is for this solution to work across all platforms.

root = tk.Tk()
app = Application(master=root)

app.master.iconbitmap("my_icon.ico")

app.mainloop()

The code is adding the default icon for a .pdf file which is not what I intended. The path to the my_icon.ico is correct. Why won't this work for Mac OS? Is there an ultimate solution that will work cross-platform?

like image 231
Jake Avatar asked Oct 16 '18 01:10

Jake


People also ask

How do I change my Python GUI icon?

To change the icon you should use iconbitmap or wm_iconbitmap I'm under the impression that the file you wish to change it to must be an ico file.

Can you use tkinter on Mac?

Tk and Tkinter apps can run on most Unix platforms. This also works on Windows and Mac OS X.


1 Answers

According to the tk tcl documentation you may want to try wm iconphoto. It appears it may support OSX and it also mentions to set the file to around a 512x512 for smooth rendering in MAC.

I do not have MAC so I cannot test this but give this a shot and let me know if it helped.

Update:

As @l'L'l pointed out you may want to try root.iconphoto(True, img). I am unable to test it myself due to not having Mac.

import tkinter as tk

root = tk.Tk()
img = tk.Image("photo", file="icon.gif")
# root.iconphoto(True, img) # you may also want to try this.
root.tk.call('wm','iconphoto', root._w, img)

root.mainloop()

Here is the relevant text from the documentation here:

wm iconphoto window ?-default? image1 ?image2 ...? Sets the titlebar icon for window based on the named photo images. If -default is specified, this is applied to all future created toplevels as well. The data in the images is taken as a snapshot at the time of invocation. If the images are later changed, this is not reflected to the titlebar icons. Multiple images are accepted to allow different images sizes (e.g., 16x16 and 32x32) to be provided. The window manager may scale provided icons to an appropriate size. On Windows, the images are packed into a Windows icon structure. This will override an ico specified to wm iconbitmap, and vice versa.

On X, the images are arranged into the _NET_WM_ICON X property, which most modern window managers support. A wm iconbitmap may exist simultaneously. It is recommended to use not more than 2 icons, placing the larger icon first.

On Macintosh, the first image called is loaded into an OSX-native icon format, and becomes the application icon in dialogs, the Dock, and other contexts. At the script level the command will accept only the first image passed in the parameters as support for multiple sizes/resolutions on macOS is outside Tk's scope. Developers should use the largest icon they can support (preferably 512 pixels) to ensure smooth rendering on the Mac.

I did test this on windows to make sure it at least works there. I used a blue square image to test.

If the above documentations is accurate it should also work on MAC.

enter image description here

like image 70
Mike - SMT Avatar answered Sep 20 '22 06:09

Mike - SMT