python打包exe

本文详细描述了如何使用Python和Tkinter构建一个简单的工具,通过调用pyinstaller将Python程序转换为可执行文件(exe)。
摘要由CSDN通过智能技术生成

python打包exe

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("gbk", errors="ignore"))
        self.output_text.insert(tk.END, stderr.decode("gbk", errors="ignore"))

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


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


if __name__ == "__main__":
    main()

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@Y.lin

别急,好运都藏在努力里~~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值