且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在python脚本中嵌入图标

更新时间:2023-12-05 19:43:34

实际上函数iconbitmap只能接收文件名作为参数,所以需要有一个文件那里。您可以在链接后面生成 Base64 版本的图标(字符串版本),然后上传将结果作为变量字符串复制并复制到源文件中。将其解压缩到临时文件,最后将该文件传递给iconbitmap并将其删除。这很简单:

Actually the function iconbitmap can only receive a filename as argument, so there needs to be a file there. You can make a Base64 version of the icon (A string version) following the link, uploading the file and copying the result in your source file as a variable string. Extract it to a temporal file, finally passing that file to iconbitmap and deleting it. It's quite simple:

import base64
import os
from Tkinter import *
##The Base64 icon version as a string
icon = \
""" REPLACE THIS WITH YOUR BASE64 VERSION OF THE ICON
"""
icondata= base64.b64decode(icon)
## The temp file is icon.ico
tempFile= "icon.ico"
iconfile= open(tempFile,"wb")
## Extract the icon
iconfile.write(icondata)
iconfile.close()
root = Tk()
root.wm_iconbitmap(tempFile)
## Delete the tempfile
os.remove(tempFile)

希望有所帮助!