Pyinstaller 打包 Tkinter 程序时引入图标解决方法
来源:互联网 发布:淘宝评价非欧什么意思 编辑:程序博客网 时间:2024/06/08 13:44
Pyinstaller 打包 Tkinter 程序时引入图标解决方法
描述
在windows下开发python的gui程序时,多数使用pyinstaller, py2exe等打包工具.
在因为tkinter框架时,代码中使用iconbitmap引入程序框的图标样式,直接运行时没有问题,
但是在pyinstaller打包后会出现无法找到ico文件问题.
网上参考了几个解决方法都过于复杂(img2py, 或者修改spec打包参数)
下面我为大家带来一个非常方便可行的办法.
解决方法
项目有两个文件分别是tk程序和图标文件
hello.pyicon.ico
hello.py源码用例
from Tkinter import *root = Tk()root.iconbitmap("icon.ico")Label(root, text="hello world").pack()root.mainloop()
运行后是一个简易的tk程序,左上角图标变为自定义的icon.ico
文件
为了适配pyinstaller能把icon文件引入,我们修改成程序,把icon.ico
修改为 icon.py
这个是img2py
带给我的启发
当前目录下进入python shell,敲入以下命令
1.以二进制方式打开icon.ico
2.进行base64编码转换
3.以变量形式保存在icon.py
中
import base64open_icon = open("icon.ico","rb")b64str = base64.b64encode(open_icon.read())open_icon.close()write_data = "img = '%s'" % b64strf = open("icon.py","w+")f.write(wrte_data)f.close()
这里可以当前目录下出现了icon.py
文件,里面只有一行就是img
变量等于一串base64
编码
hello.pyicon.icoicon.py
修改 hello.py源码,导入icon.py
中的img
, 创建一个临时的tmp.ico
文件作为图标引入后删除即可
from Tkinter import *import base64from icon import imgroot = Tk()tmp = open("tmp.ico","wb+")tmp.write(base64.b64decode(img))tmp.close()root.iconbitmap("tmp.ico")os.remove("tmp.ico")Label(root, text="hello world").pack()root.mainloop()
最后就是pyinstaller打包
pyinstaller -i icon.ico -w -F hello.py
0 0
- Pyinstaller 打包 Tkinter 程序时引入图标解决方法
- pyinstaller 打包程序 出现错误的解决方法
- PyInstaller打包python程序
- Python程序打包--PyInstaller
- python 打包程序 pyinstaller py2exe
- PyInstaller-2.1打包python程序
- Pyinstaller打包生成可执行程序
- 利用pyinstaller打包python3程序
- 使用PyInstaller打包Python程序
- PyInstaller打包Python程序详解
- pyinstaller打包sqlalchemy问题的解决方法
- pyinstaller打包apscheduler问题的解决方法
- 使用tkinter和pyinstaller开发一个GUI爬虫程序
- python程序打包工具py2exe/pyInstaller
- 采用pyinstaller 2.0打包python程序
- 使用 pyinstaller 打包 Python 程序为可执行文件
- python程序打包(py2exe, pyinstaller, cxfreeze)
- pyinstaller的使用python 打包程序
- Objective-C的属性和成员变量用法及关系浅析
- VMware 虚拟机迁移
- Android开发记录
- c++第四次作业 继承和派生
- CBCharacteristic-CBMutableCharacteristic学习笔记
- Pyinstaller 打包 Tkinter 程序时引入图标解决方法
- Python-安装feedparser
- 设计模式C++学习笔记之七(AbstractFactory抽象工厂模式)
- EditTextView的介绍和使用
- CBService-CBMutableService学习笔记
- 使用Command Line(终端)提交代码到远程库
- [一起学Hive]之三–Hive中的数据库(Database)和表(Table)
- Android 给图片、文字、控件、布局添加阴影效果(悬浮效果)
- 一个ListView使用多个布局文件