python - 在 Mac OS 上设置 tkinter 图标

标签 python macos tkinter icons cross-platform

我正在尝试更改出现在我的 Mac 操作系统 tk 应用程序上的图标。上次我检查此代码适用于 Windows。目标是让该解决方案适用于所有平台。

root = tk.Tk()
app = Application(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,我无法自行测试。

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()

这是来自 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.

我确实在 Windows 上对此进行了测试,以确保它至少可以在 Windows 上运行。我使用蓝色方形图像进行测试。

如果上述文档准确无误,它应该也适用于 MAC。

enter image description here

关于python - 在 Mac OS 上设置 tkinter 图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52826692/

相关文章:

python - 为什么照片图像不存在?

单击矩形时的python tkinter Canvas

python - 将一组 n 个整数转换为一个 n 个整数列表的时间复杂度是多少?

python - iPython 启动时抛出错误 : "ipythonrc not found"

macos - 如何从歌曲文件中获取歌曲的歌词

python - 如何在 Tkinter 中使用条目小部件

python - setUpClass可以在unnitest框架中扩展吗?

Python 使用 pandas 将 xlsx 转换为 csv 文件。如何删除索引列?

python - 适应 matplotlib 中的协方差等高线绘图示例会引发奇异矩阵错误

objective-c - 用于 Cocoa OS x 的语音到文本转录 API