PyInstaller 打包pc

PyInstaller 是一个将 Python 脚本转换为独立可执行文件的工具。它可以自动分析 Python 脚本及其依赖项,并将所有需要的文件打包到一个文件夹或单个可执行文件中。这对于希望分发 Python 程序给不具备 Python 环境的用户来说非常有用。

安装PyInstaller

bash 复制代码
pip install pyinstaller

使用PyInstaller打包

bash 复制代码
pyinstaller --onefile your_script.py

pyinstaller 隐藏命令行窗口

bash 复制代码
pyinstaller --onefile --windowed your_script.py

设置应用图标

bash 复制代码
pyinstaller -F -i logo_favicon-wechat.ico --name=main main.py

PyInstaller常用参数

  • --name : 指定可执行文件名称。
  • --specpath: 指定spec文件的生成目录,默认为当前目录。
  • -F, --onefile: 将整个应用程序打包到单个可执行文件中。
  • -D, --onedir: 生成一个目录,包含多个文件。
  • --add-data 、 --add-binary: 在构建中插入额外的数据或二进制文件,可用于绑定配置文件、示例或其他非代码数据。
  • --exclude-module: 排除某些模块。
  • -d, --debug: 提供debug输出。
  • -w, --windowed, --noconsole: 关闭控制台窗口(仅对Windows有效)。
  • -c, --nowindowed, --console: 使用命令行窗口(仅对Windows有效)。
  • -i: 设置应用图标。

demo

index.py

python 复制代码
# 导入tkinter库
import tkinter as tk
# 导入ttkbootstrap库
from tkinter import ttk
from ttkbootstrap import Style
# 创建主窗口,并使用minty主题
style = Style(theme='minty')
window = style.master
window.title('简易计算器')
window.geometry('300x400')
# 定义一个字符串变量,用于显示计算结果
result = tk.StringVar()
result.set(0)
# 创建一个标签,用于显示结果
label = tk.Label(window, bg='lightgreen', width=25, textvariable=result)
label.place(x=5, y=5)
# 定义一个列表,存储按钮的文本
buttons = ['Clear', 'Del', '%', '/',
           '7', '8', '9', '*',
           '4', '5', '6', '-',
           '1', '2', '3', '+',
           '.', '0', '=']
# 定义一个函数,用于处理按钮的点击事件
def click(event):
    global result # 声明全局变量
    value = event.widget['text'] # 获取按钮的文本
    if value == '=': # 如果是等号,就计算表达式的值,并显示结果
        try:
            result.set(result.get() + '=' + str(eval(result.get())))
        except:
            result.set('Error')
    elif value == 'Clear': # 如果是清除键,就清空结果
        result.set(0)
    elif value == 'Del': # 如果是删除键,就删除最后一个字符
        if result.get() != 0:
            result.set(result.get()[:-1])
    else: # 其他情况,就在结果后面追加字符
        if result.get() == '0':
            result.set(value)
        else:
            result.set(result.get() + value)
# 循环创建按钮,并绑定点击事件,并设置按钮的形状和大小为圆角和大号(rounded-lg)
x = 5 # 按钮的初始横坐标
y = 50 # 按钮的初始纵坐标
for i in range(len(buttons)):
    button = ttk.Button(window, text=buttons[i], width=6, style='success.TButton') # 创建按钮对象,并指定样式为rounded-lg.TButton 
    button.place(x=x, y=y) # 放置按钮对象到窗口上 
    button.bind('<Button-1>', click) # 绑定点击事件到函数click上
    x += 70 # 更新横坐标
    if (i + 1) % 4 == 0: # 如果是每行的最后一个按钮,就换行,并重置横坐标和纵坐标 
        x = 5 
        y += 50
# 启动主循环        
window.mainloop()

导包命令

bash 复制代码
pyinstaller --onefile --windowed index.py
相关推荐
fish_study_csdn20 分钟前
pytest 技术总结
开发语言·python·pytest
咖啡调调。29 分钟前
使用Django框架表单
后端·python·django
BO_S__32 分钟前
python调用ffmpeg对截取视频片段,可批量处理
python·ffmpeg·音视频
就叫飞六吧1 小时前
如何判断你的PyTorch是GPU版还是CPU版?
人工智能·pytorch·python
pyengine2 小时前
基于pandoc的MarkDown格式与word相互转换小工具开发(pyqt5)
开发语言·python·qt·word
YuSun_WK2 小时前
配置MambaIRv2: Attentive State Space Restoration的环境
开发语言·python
Nick_zcy2 小时前
开发基于python的商品推荐系统,前端框架和后端框架的选择比较
开发语言·python·前端框架·flask·fastapi
一点.点3 小时前
李沐动手深度学习(pycharm中运行笔记)——04.数据操作
pytorch·笔记·python·深度学习·pycharm·动手深度学习
Niuguangshuo3 小时前
Python 设计模式:访问者模式
python·设计模式·访问者模式
Jamesvalley3 小时前
【Django】新增字段后兼容旧接口 This field is required
后端·python·django