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
相关推荐
神仙别闹4 分钟前
基于Python实现LSTM对股票走势的预测
开发语言·python·lstm
机器学习之心34 分钟前
小波增强型KAN网络 + SHAP可解释性分析(Pytorch实现)
人工智能·pytorch·python·kan网络
JavaEdge在掘金40 分钟前
MySQL 8.0 的隐藏索引:索引管理的利器,还是性能陷阱?
python
站大爷IP1 小时前
Python办公自动化实战:手把手教你打造智能邮件发送工具
python
chao_7891 小时前
回溯题解——子集【LeetCode】二进制枚举法
开发语言·数据结构·python·算法·leetcode
zdw1 小时前
fit parse解析佳明.fit 运动数据,模仿zwift数据展示
python
剑桥折刀s2 小时前
Python打卡:Day46
python
巴里巴气2 小时前
Python爬虫图片验证码和滑块验证码识别总结
爬虫·python
sword devil9003 小时前
PYQT实战:智能家居中控
python·智能家居·pyqt
NetX行者3 小时前
FastMCP:用于构建MCP服务器的开源Python框架
服务器·python·开源