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
相关推荐
databook6 小时前
Manim实现闪光轨迹特效
后端·python·动效
Juchecar8 小时前
解惑:NumPy 中 ndarray.ndim 到底是什么?
python
用户8356290780518 小时前
Python 删除 Excel 工作表中的空白行列
后端·python
Json_8 小时前
使用python-fastApi框架开发一个学校宿舍管理系统-前后端分离项目
后端·python·fastapi
数据智能老司机14 小时前
精通 Python 设计模式——分布式系统模式
python·设计模式·架构
数据智能老司机15 小时前
精通 Python 设计模式——并发与异步模式
python·设计模式·编程语言
数据智能老司机15 小时前
精通 Python 设计模式——测试模式
python·设计模式·架构
数据智能老司机15 小时前
精通 Python 设计模式——性能模式
python·设计模式·架构
c8i16 小时前
drf初步梳理
python·django
每日AI新事件16 小时前
python的异步函数
python