我在使用pyinstaller将代码转换为可执行文件时遇到问题。这是一段使用tkfilebrowser选择目录并打印它们代码。如果你在python上试用它,它运行起来没有大的问题。但是,如果您尝试将其设置为可执行文件,那么当您尝试选择一个目录时,它将不起作用,并出现错误:“_tkinter.TclError: you‘t open "C:Users...\tkfilebrowser...\file.png":没有这样的文件或目录”。如果有任何帮助,我将不胜感激。下面是关于代码和bug的更多详细信息。
我的简单代码如下:
import tkinter as tk
import tkfilebrowser
root = tk.Tk()
canvas = tk.Canvas(root, height=600, width=1133, bg="black")
canvas.pack()
frame = tk.Frame(root, bg="white")
frame.place(relwidth=0.96, relheight=0.6, relx=.02,rely=0.28)
dirs=[]
def get_directories():
aux=[]
aux.append(tkfilebrowser.askopendirnames(initialdir="/"))
for i in aux[0]:
dirs.append(i)
print(dirs)
return dirs
selectFolder = tk.Button(root, text= '1 - Select Folder', padx=5, pady=5, fg="white",
bg="#263D42", command = get_directories)
selectFolder.pack(side=tk.RIGHT)
root.mainloop()
为了将其转换为可执行文件,我在cmd上使用了以下命令(我将文件保存为"test"):
pyinstaller.exe --onefile --test.py
您可以在下图中看到该错误。
发布于 2021-07-15 10:55:18
问题是pyinstaller没有导入tkfilebrowser模块。您应该手动导入。请使用以下代码将路径添加到spec文件中的tkfilebrowser文件夹:
# test.spec file
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
a.datas += Tree("C:\\Users\\I_like_chocolate\\Miniconda3\\Lib\\site-
packages\\tkfilebrowser\\", "tkfilebrowser") # !!! Path to add
在再次运行之前,请确保对test.spec文件进行保存。然后,您应该在命令中运行test.spec文件,而不是运行pyinstaller.exe --onefile --test.py
命令:
# cmd
pyinstaller.exe test.spec
https://stackoverflow.com/questions/68384747
复制相似问题