1.命令行Terminal安装pyinstaller
pyinstaller --onefile --noconsole calculator.py
2.简单做个计算器Demo 文件名称为 calculator.py
import tkinter as tk
def calculate(event=None):
try:
result = eval(entry.get())
label.config(text=str(result))
except:
label.config(text="Invalid input")
root = tk.Tk()
root.title("Simple Calculator")
entry = tk.Entry(root, width=35, borderwidth=5)
entry.bind("<Return>", calculate)
entry.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
label = tk.Label(root, text="")
label.grid(row=1, column=0, columnspan=3)
button_clear = tk.Button(root, text="Clear", padx=79, pady=20)
button_clear.bind("<Button-1>", lambda event: entry.delete(0, tk.END))
button_clear.grid(row=2, column=1, sticky=tk.W+tk.E)
root.mainloop()
3.在Terminal命令行中使用命令 标明文件名
–onefile 选项将生成一个单个的可执行文件。
–noconsole 选项将在运行时不显示控制台窗口。
pyinstaller --onefile --noconsole calculator.py
4.当打包完成后,在生成的dist目录中会有一个.exe文件,这就是你的可执行文件