Wong's Blog

Always on the way forward
  1. 首页
  2. Windows
  3. 正文

Python Pyinstaller打包exe左上角图标显示的解决方案

2024 年 3 月 29 日 399点热度 4条评论

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
标签: 暂无
最后更新:2024 年 3 月 29 日

wong

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

文章评论

  • zoritoler imol

    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.

    2025 年 3 月 12 日
    回复
  • vorbelutrioperbir

    I consider something really interesting about your site so I bookmarked.

    2025 年 4 月 1 日
    回复
  • drover sointeru

    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!

    2025 年 4 月 4 日
    回复
  • Water Damage Mitigation Atlanta

    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.

    2025 年 6 月 5 日
    回复
  • razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
    取消回复
    • Linux / 6篇
    • Macos / 7篇
    • Windows / 9篇
    • 其他 / 9篇

    在路上 | 2024