将.py/.pyw转换为.exe [英] Convert .py/.pyw to .exe
本文介绍了将.py/.pyw转换为.exe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从 .py
或 .pyw
文件创建一个 .exe
文件.
I want to create an .exe
file from a .py
or .pyw
file.
我该怎么做?(带有 cx-freeze 的问题)
How can I do this? (with cx-freeze)
推荐答案
编码将要转换的文件
- 下载
cx_Freeze
(如果您未这样做)此处. - 创建一个新的
Python
文件并粘贴以下代码:
- Download
cx_Freeze
(if you didn't do that) here. - Create a new
Python
file and paste the following code:
import os
import time
from tkinter import *
from tkinter.filedialog import askopenfile
from tkinter.scrolledtext import ScrolledText
from tkinter.messagebox import *
tk = Tk()
tk.title(".py -> .exe")
tk.resizable(0, 0)
f = None # file chosen
def browse():
global f, btn
try:
f = askopenfile().name # get the path of the chosen file
btn["text"] = os.path.basename(f)
except:
f = None
def convert():
global f, btn, ver, des
OK = False
try:
dots = 0
for x in ver.get():
if x == ".":
dots += 1
else:
x = int(x)
if dots < 4:
OK = True # check the number of dots in the version
except:
showwarning("","The version must be int.int.int... with max 3 dots.")
if OK:
try:
if f is None:
showwarning("","You must choose a file to convert.")
btn.focus()
elif ver.get() == "":
showwarning("","You must enter a version.")
ver.focus()
else:
# create and fill the launch files
with open("setup.py", "w") as f_:
f_.write("NAME = '" + f +
"'\nVERSION = '" + ver.get() +
"'\nDESCRIPTION = \"\"\"" + des.get(1.0, "end") +
"\"\"\"\n\nfrom cx_Freeze import setup, Executable\nsetup(name = NAME, version = VERSION, description = DESCRIPTION, executables = [Executable(NAME)])")
with open("start.bat", "w") as f_:
f_.write("py setup.py build")
os.system("start.bat") # run the launch file
os.remove("setup.py") # remove the created files
os.remove("start.bat") #
showinfo("Information","End. Your exe file is in folder 'build'.")
except:
showerror("Error","Unknown error detected.") # any unknown error
# GUI
Label(text="File to convert").grid(column=0, row=0, sticky="w")
btn = Button(text="Browse...", command=browse)
btn.grid(column=1, row=0)
Label(text="Version").grid(column=0, row=2, sticky="w")
ver = Entry(width=23)
ver.grid(column=1, row=2, padx=5)
ver.insert(0, "1.0")
Label(text="Description").grid(column=0, row=3, sticky="w")
des = ScrolledText(width=15, height=5, wrap=WORD)
des.grid(column=1, row=3)
Label(text="Convert to .exe").grid(column=0, row=4, sticky="w")
Button(text="Convert", command=convert).grid(column=1, row=4, pady=5)
tk.mainloop()
-
运行代码.选择一个文件.点击
转换
按钮.
命令提示符窗口可让您查看进度.
A command prompt window allows you to see the progress.
错误
- 命令提示符在很短的时间内保持打开状态
-
更改
Change
with open("start.bat", "w") as f_: f_.write("py setup.py build")
作者
with open("start.bat", "w") as f_: f_.write("py setup.py build") f_.write("pause")
然后,在Internet上搜索错误.
Then, search the error on the Internet.
检查是否已选择
.py
或.pyw
文件.Check if you have selected a
.py
or.pyw
file.- 文件夹"build"未创建
- 检查要转换的文件的路径:它是否包含加重字符,一些空格?>>例如,将文件放在您的桌面或闪存驱动器中即可.
- 检查说明:它是否包含强调字符?删除它们.
- 您是否已安装
cx_Freeze
? - 您无法打开您的
.exe
文件 - 如果您的代码需要其他文件(例如图像,音乐),则将其复制到当前文件夹中.
- 您检查过文件吗?它包含一些错误吗?
- 如果您使用
tkinter
:您是否已循环窗口? - If your code needs others files, like images, musics, then copy them in the current folder.
- Have you checked your file? Does it contains some errors?
- If you use
tkinter
: have you looped the window?
这篇关于将.py/.pyw转换为.exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文