在Mac OS上设置tkinter图标 [英] Set tkinter icon on Mac OS
问题描述
我正在尝试更改Mac OS的tk应用程序中显示的图标.我上次检查此代码对Windows有效.该解决方案的目标是在所有平台上都能正常工作.
root = tk.Tk()app =应用程序(master = root)app.master.iconbitmap("my_icon.ico")app.mainloop()
代码为.pdf文件添加了默认图标,这不是我想要的. my_icon.ico
的路径正确.为什么这不适用于Mac OS?是否有最终的解决方案可以跨平台使用?
根据 tk tcl
文档,您可能想尝试 wm iconphoto
.看来它可能支持OSX,并且还提到将文件设置为512x512左右,以便在MAC中平滑渲染.
我没有MAC,因此无法测试,但请试一下并告诉我是否有帮助.
更新:
正如@ l'L'l所指出的,您可能想尝试 root.iconphoto(True,img)
.由于没有Mac,我无法自己对其进行测试.
将tkinter导入为tk根= tk.Tk()img = tk.Image("photo",file ="icon.gif")#root.iconphoto(True,img)#您可能还想尝试一下.root.tk.call('wm','iconphoto',root._w,img)root.mainloop()
这是
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?
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.
这篇关于在Mac OS上设置tkinter图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!