gpt4 book ai didi

python - 在 Mac OS 上设置 tkinter 图标

转载 作者:太空狗 更新时间:2023-10-29 21:51:43 24 4
gpt4 key购买 nike

我正在尝试更改出现在我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com