python打包工具

admin

文章最后更新时间:2023年05月25日

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中。

下载地址:

网络财神 此部分为隐藏内容,请输入验证码后查看
验证码:


扫描右侧图片,或微信搜索 “ 网络财神 ” 或 “ newjixi ” 关注本站官方公众号,回复 “ 验证码 ” ,获取验证密码。
文章版权声明:除非注明,否则均为执刀人的工具库原创文章,转载或复制请以超链接形式并注明出处。

发表评论

快捷回复: 表情:
AddoilApplauseBadlaughBombCoffeeFabulousFacepalmFecesFrownHeyhaInsidiousKeepFightingNoProbPigHeadShockedSinistersmileSlapSocialSweatTolaughWatermelonWittyWowYeahYellowdog
验证码
评论列表 (暂无评论,228人围观)

还没有评论,来说两句吧...

目录[+]

取消
微信二维码
微信二维码
支付宝二维码