tkinter TclError: error reading bitmap file

前端 未结 5 2101
天命终不由人
天命终不由人 2020-11-28 07:53

I am trying to set an application icon (python3 / tkinter) like this:

Interface()
root.title(\"Quicklist Editor\")
root.iconbitmap(\'@/home/jacob/.icons/qle_         


        
5条回答
  •  温柔的废话
    2020-11-28 08:03

    This is an old question, and there is lots of stuff written about it on the web, but all of it is either incorrect or incomplete, so having gotten it to work I thought it would be good to record my actual working code here.

    First, you'll need to create an icon and save it in two formats: Windows "ico" and Unix "xbm". 64 x 64 is a good size. XBM is a 1-bit format--pixels just on or off, so no colors, no grays. Linux implementations of tkinter only accept XBM even though every Linux desktop supports real icons, so you're just out of luck there. Also, the XBM spec is ambiguous about whether "on" bits represent black or white, so you may have to invert the XBM for some desktops. Gimp is good for creating these.

    Then to put the icon in your titlebar, use this code (Python 3):

    import os
    from tkinter import *
    from tkinter.ttk import *
    
    root = Tk()
    root.title("My Application")
    if "nt" == os.name:
        root.wm_iconbitmap(bitmap = "myicon.ico")
    else:
        root.wm_iconbitmap(bitmap = "@myicon.xbm")
    
    root.mainloop()
    

提交回复
热议问题