1. 找到exe运行时资源文件的绝对路径
sys._MEIPASS是pyinstaller打包的exe运行时创建临时目录的绝对路径,需要在程序运行时判断当前是不是有这个属性,如果有的话,生成临时目录的绝对路径,如果没有的话,那就直接返回当前程序的绝对路径:
import os,sys
def get_resource_path(relative_path):
if hasattr(sys,'_MEIPASS'):
return os.path.join(sys._MEIPASS,relative_path)
return os.path.join(os.path.abspath("."),relative_path)
2. 加载资源
root.iconbitmap(get_resource_path('ico\sms3.ico'))
3. Pyinstaller打包时引入图片资源
--add-data="image1.png:img" 的意思是把当前目录里一个叫“image1.png”的文件打包进去,但是放在打包后的“img”目录下,也就是变成img/image1.png,文件名不变。
--add-data 'images:images' 也就是把当前目录下images文件夹里的文件都打包进去,打包后的目录也是images一样的文件夹下。
Pyinstaller打包代码:
pyinstaller -F -w -i .\sms3.ico --add-data="sms3.ico:ico" .\xxx.py
文章评论
I will right away take hold of your rss as I can't to find your email subscription link or newsletter service. Do you have any? Kindly permit me recognize in order that I could subscribe. Thanks.
I consider something really interesting about your site so I bookmarked.
Hello! I could have sworn I’ve been to this blog before but after browsing through some of the post I realized it’s new to me. Anyways, I’m definitely happy I found it and I’ll be book-marking and checking back frequently!
My partner and I stumbled over here coming from a different web page and thought I might check things out. I like what I see so now i am following you. Look forward to going over your web page yet again.
Este site é realmente incrível. Sempre que acesso eu encontro coisas diferentes Você também vai querer acessar o nosso site e descobrir detalhes! conteúdo único. Venha descobrir mais agora! :)
Unquestionably believe that which you said. Your favorite reason appeared to be on the web the simplest thing to be aware of. I say to you, I definitely get annoyed while people think about worries that they just don't know about. You managed to hit the nail upon the top as well as defined out the whole thing without having side effect , people can take a signal. Will likely be back to get more. Thanks