我可以为 tkinter 图标使用哪些文件格式?

Which file formats can I use for tkinter icons?

我知道这可能很明显,但在 tkinter 中你可以设置一个图标,但我发现很难找到一个。我只是想知道您是否必须对文件使用 .ico 格式,或者是否有办法使用 .png.jpeg 文件。

目前我有

window = Tkinter.Tk()
window.title("Weclome!")
window.geometry("200x300")
window.wm_iconbitmap("Icon.ico")
window.configure(background = "Black")

这就是我的全部设置,我只想知道第 4 行:

window.wm_iconbitmap("Icon.ico") 

感谢您回答我的问题,虽然我很抱歉没有花更多的时间来研究这个问题,而只是在这里提问。

让我们从阅读文档开始吧!

effbot.org 上的文档说明了以下关于 iconbitmap(bitmap=None)

的内容

Sets or gets the icon bitmap to use when this window is iconified. This method is ignored by some window managers (including Windows).

Note that this method can only be used to display monochrome icons. To display a color icon, put it in a Label widget and display it using the iconwindow method instead.

Same as wm_iconbitmap.

所以这是关于 iconwindow(window=None) 的文档:

Sets or gets the icon window to use as an icon when this window is iconified. This method is ignored by some window managers (including Windows).

Same as wm_iconwindow.

window

The new icon window. If omitted, the current window is returned.

根据这个 other documentation,它实际上与 tkinter 在(至少)Python 2.7、3.5 和 3.6 中的同名方法的文档字符串说的相同:

wm_iconbitmap(self, bitmap=None, default=None)

Set bitmap for the iconified widget to bitmap. Return the bitmap if None is given.

Under Windows, the default parameter can be used to set the icon for the widget and any descendents that don't have an icon set explicitly. default can be the relative path to a .ico file (example: root.iconbitmap(default='myicon.ico') ). See Tk documentation for more information.

这是原始的 Tk 文档:

wm iconbitmap window ?bitmap?

If bitmap is specified, then it names a bitmap in the standard forms accepted by Tk (see the Tk_GetBitmap manual entry for details). This bitmap is passed to the window manager to be displayed in window's icon, and the command returns an empty string. If an empty string is specified for bitmap, then any current icon bitmap is canceled for window. If bitmap is specified then the command returns an empty string. Otherwise, it returns the name of the current icon bitmap associated with window, or an empty string if window has no icon bitmap.

根据我对 Tcl 的理解,此处 window 是您的顶层 window(TkToplevel 的实例)。

On the Windows operating system, an additional flag is supported:

wm iconbitmap window ?-default? ?image?

If the -default flag is given, the icon is applied to all toplevel windows (existing and future) to which no other specific icon has yet been applied.

In addition to bitmap image types, a full path specification to any file which contains a valid Windows icon is also accepted (usually .ico or .icr files), or any file for which the shell has assigned an icon.

Tcl will first test if the file contains an icon, then if it has an assigned icon, and finally, if that fails, test for a bitmap.

目前回答不是很具体,因此很有帮助。


我的结论

当 window 图标化时,应该使用 iconbitmap 函数(或方法,取决于编程语言)将 bitmap 图像设置为 window .

在 Windows 上,您可以为包含 valid Windows icon 的任何文件设置完整路径规范也被接受(通常是 .ico.icr 文件) ,或 shell 为其分配了图标的任何文件。

那么哪些图像是位图?

  1. xbm and xpm(对于X Window系统

    根据我在上面链接 "bitmap" 的 Wikipedia article

    The X Window System uses a similar xbm format for black-and-white images, and xpm for color images. ...

  2. BMP file format

  3. Netpbm format

  4. .wbmp

  5. ILBM

    ...

所以大多数位图文件格式不能跨平台!换句话说,如果有人告诉您使用 xbm 图像作为图标,它可能无法在您的平台上运行,因为 xbmX Window System.

注意:即使在这个答案之后你可能仍然有问题!


其他可能有用的文章

  • Set window icon
  • tkinter TclError: error reading bitmap file

我也是费了好大劲才找到答案,但最后我偷看了idle3.6的源代码,在那里我找到了以下代码:

# set application icon
icondir = os.path.join(os.path.dirname(__file__), 'Icons')
if system() == 'Windows':
    iconfile = os.path.join(icondir, 'idle.ico')
    root.wm_iconbitmap(default=iconfile)
else:
    ext = '.png' if TkVersion >= 8.6 else '.gif'
    iconfiles = [os.path.join(icondir, 'idle_%d%s' % (size, ext))
                 for size in (16, 32, 48)]
    icons = [PhotoImage(master=root, file=iconfile)
             for iconfile in iconfiles]
    root.wm_iconphoto(True, *icons)

我使用 rummage 软件在 idlelib 文件夹中的所有文件中搜索了 .ico 和 .png。

所以最后我设法让 window 图标工作(在 GNU-linux 上,TkVersion>=8.6)用下面两行:

icon = PhotoImage(master=root, file='icon.png')
root.wm_iconphoto(True, icon)

我把图标直接放在我的应用程序文件夹中。

从空闲代码看来,Windows 仍然只支持 .ico 文件。