python打包工具

将python程序文件打包成exe文件的工具
代码如下:

import subprocess
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.master.title("Python打包工具")
        self.pack()
        self.create_widgets()

    def create_widgets(self):
        # 创建一个标签和一个按钮
        self.filename_label = tk.Label(self, text="请选择Python程序:")
        self.filename_label.pack()
        self.select_button = tk.Button(self, text="选择文件", command=self.select_file)
        self.select_button.pack()

        # 创建一个按钮,单击它将运行命令
        self.convert_button = tk.Button(self, text="转换为exe", command=self.convert_to_exe, state=tk.DISABLED)
        self.convert_button.pack()

        # 创建一个文本框,用于显示输出
        self.output_text = tk.Text(self)
        self.output_text.pack()

    def select_file(self):
        # 打开文件选择对话框
        file_path = filedialog.askopenfilename()

        # 如果用户选择了文件,则启用转换按钮并更新文件名标签的文本
        if file_path:
            self.filename_label.config(text=f"已选择的文件:{file_path}")
            self.selected_file = file_path
            self.convert_button.config(state=tk.NORMAL)

    def convert_to_exe(self):
        # 获取已选择的Python程序的路径
        filename = self.selected_file

        # 构造命令
        command = f"pyinstaller --onefile {filename}"

        # 运行命令
        process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

        # 获取输出
        stdout, stderr = process.communicate()

        # 在文本框中显示输出
        self.output_text.delete("1.0", tk.END)
        self.output_text.insert(tk.END, stdout.decode())
        self.output_text.insert(tk.END, stderr.decode())

        # 根据转换结果弹出消息框
        if process.returncode == 0:
            messagebox.showinfo("提示", "转换成功!")
        else:
            messagebox.showerror("错误", "转换失败!")

def main():
    root = tk.Tk()
    app = Application(master=root)
    app.mainloop()

if __name__ == "__main__":
    main()

软件截图:

使用过程

:选择python程序文件,点击转换成exe。

注意事项:生成的exe文件在该软件或程序的文件夹dist中。

链接:

https://pan.baidu.com/s/1DX9eoGDchOosCijtUlbOiQ 提取码:52pj

© 版权声明
THE END
如果内容对您有所帮助,就支持一下吧!
点赞0 分享
评论 共46条

请登录后发表评论