Why isn't .ico file defined when setting window's icon?

前端 未结 12 1567
一整个雨季
一整个雨季 2020-11-27 15:47

When I tried to change the window icon in the top left corner from the ugly red \"TK\" to my own favicon using the code below, Python threw an error:

from tk         


        
12条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-27 16:07

    You need to have favicon.ico in the same folder or dictionary as your script because python only searches in the current dictionary or you could put in the full pathname. For example, this works:

    from tkinter import *
    root = Tk()
    
    root.iconbitmap(r'c:\Python32\DLLs\py.ico')
    root.mainloop()
    

    But this blows up with your same error:

    from tkinter import *
    root = Tk()
    
    root.iconbitmap('py.ico')
    root.mainloop()
    

提交回复
热议问题