Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.3k views
in Technique[技术] by (71.8m points)

python - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...