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 分钟前
Win11 安装 Visual Studio(保姆教程 - 更新至2025.07)
c++·ide·vscode·python·visualstudio·visual studio
m0_6256865537 分钟前
day53
python
Real_man1 小时前
告别 requirements.txt,拥抱 pyproject.toml和uv的现代Python工作流
python
站大爷IP2 小时前
Python文件操作的"保险箱":with语句深度实战指南
python
运器1232 小时前
【一起来学AI大模型】算法核心:数组/哈希表/树/排序/动态规划(LeetCode精练)
开发语言·人工智能·python·算法·ai·散列表·ai编程
巴里巴气4 小时前
selenium基础知识 和 模拟登录selenium版本
爬虫·python·selenium·爬虫模拟登录
19894 小时前
【零基础学AI】第26讲:循环神经网络(RNN)与LSTM - 文本生成
人工智能·python·rnn·神经网络·机器学习·tensorflow·lstm
JavaEdge在掘金4 小时前
Redis 数据倾斜?别慌!从成因到解决方案,一文帮你搞定
python
ansurfen4 小时前
我的第一个AI项目:从零搭建RAG知识库的踩坑之旅
python·llm
前端付豪5 小时前
20、用 Python + API 打造终端天气预报工具(支持城市查询、天气图标、美化输出🧊
后端·python