我想从
.exe
文件创建一个.py
文件。我该怎么做?
有很多不同的方法。
我更喜欢使用cx_Freeze
,因为使用它创建.exe
文件既简单又快速。
答案 0 :(得分:2)
-----创建文件-----
cx_Freeze
(如果您没有这样做的话)。Python
文件并粘贴以下代码:。
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()
运行代码。选择一个文件。点击convert
按钮。
命令提示符窗口可让您查看进度。
您的.exe
文件已创建!
-----错误-----
更改
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上搜索错误。
检查您是否选择了.py
或.pyw
文件。
cx_Freeze
吗?.exe
文件无法打开? tkinter
:是否已循环窗口?一个消息框告诉您检测到未知错误?
对不起,在这种情况下我不能帮您...