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
相关推荐
森哥的歌22 分钟前
Python uv包管理器使用指南:从入门到精通
python·开发工具·uv·虚拟环境·包管理
qq_2147826125 分钟前
给你的matplotlib images添加scale Bar
python·数据分析·matplotlib
Johny_Zhao36 分钟前
Vmware workstation安装部署微软SCCM服务系统
网络·人工智能·python·sql·网络安全·信息安全·微软·云计算·shell·系统运维·sccm
waterHBO38 分钟前
python + flask 做一个图床
python
ZWaruler2 小时前
二: 字典及函数的使用
python
蚰蜒螟2 小时前
深入解析JVM字节码解释器执行流程(OpenJDK 17源码实现)
开发语言·jvm·python
墨绿色的摆渡人2 小时前
pytorch小记(二十):深入解析 PyTorch 的 `torch.randn_like`:原理、参数与实战示例
人工智能·pytorch·python
英英_3 小时前
python 自动化教程
开发语言·python·自动化
万能程序员-传康Kk3 小时前
【Python+flask+mysql】网易云数据可视化分析(全网首发)
python·mysql·信息可视化·数据分析·flask·可视化·网易云
先做个垃圾出来………3 小时前
汉明距离(Hamming Distance)
开发语言·python·算法