步骤1:
修改sTargetPicPath图片路径为自己图片的路径,先运行如下代码,得到图片的base64格式的图片码值
import base64
sTargetPicPath = r'C:\Users\user\Desktop\1.jpg'
with open(sTargetPicPath, 'rb') as open_icon:
b64str = base64.b64encode(open_icon.read())
print(b64b64str)
步骤2:
修改b63img的值为步骤1得到的值,运行如下代码例子,设置应用图标,大功告成。
import tkinter as tk
from base64 import b64decode
from PIL import Image, ImageTk
window = tk.Tk()
window.title("test")
window.geometry("420x200+0+0")
b64img = b'xxx'
icon_img = b64decode(b64img)
icon_img = ImageTk.PhotoImage(data=icon_img)
window.tk.call('wm','iconphoto',window,icon_img)
if __name__ == "__main__":
window.mainloop()