【问题标题】:after converting my tkinter py file to exe i get an error将我的 tkinter py 文件转换为 exe 后出现错误
【发布时间】:2020-07-19 13:40:18
【问题描述】:

只要我将它作为 .py 文件运行,我的程序就可以完美运行,但在转换为 exe 后出现此错误

_tkinter.TclError: couldn't open "C:\Users\MYNAME~1\AppData\Local\Temp\_MEI107362\tkfilebrowser\images\file.png": no such file or directory 

为什么要在这里打开那个文件是我的代码

 name = askopenfilename( parent=root, initialdir='/', initialfile='tmp', \
 filetypes=[("JPG", "*.jpg"), ("MP3", "*.mp3"), ("TXT", "*.txt")])

我使用 pyinstaller 进行转换

【问题讨论】:

  • 我不确定该错误是否与您指出的行有关。 file.png 是否可能是您用于 GUI 的图像?
  • 不,我没有,我什至没有一个叫做那个的文件,错误中显示的路径不是我机器的真实路径
  • 我还有其他三个函数做同样的事情,它们都作为脚本工作,但不在 exe 中
  • _MEI107362 是 pyinstaller bootloader 解压 exe 文件中所有内容的临时文件。您的错误告诉我,可能有一段代码正在尝试使用使用 __file__ 构建的相对路径来访问 file.png 以确定当前路径。这在 MHO 中没有链接到 askopenfilename。像往常一样,创建一个最小可复制示例会很有帮助。
  • from tkinter import * from tkfilebrowser import askopenfilename root = Tk() def tri(): name = askopenfilename( parent=root, initialdir='\\', initialfile='tmp', \ filetypes= [("JPG", ".jpg"), ("MP3", ".mp3"), ("TXT", "*.txt")]) 按钮(root , text="试试", command = tri).pack() root.geometry("800x800")

标签: python tkinter pyinstaller


【解决方案1】:

这是因为不应使用\ 打开文件。打开文件的正确字符串是\\。这来自\ 被用来转义字符,python 就是这样解释的。

 name = askopenfilename( parent=root, initialdir='\\', initialfile='tmp', \
 filetypes=[("JPG", "*.jpg"), ("MP3", "*.mp3"), ("TXT", "*.txt")])

【讨论】:

  • 我改变了它,我仍然得到同样的错误任何其他方法可以尝试吗?提前致谢
  • 这是我的错误:Tkinter 回调 Traceback 中的异常(最近一次调用最后一次):文件“tkinter_init_.py”,第 1883 行,在 call 文件“try.py”,第 13 行,在 tri 文件“site-packages\tkfilebrowser\functions.py”,第 142 行,在 askopenfilename 文件“site-packages\tkfilebrowser\filebrowser.py”,第 210 行,在 init 文件“tkinter_init_.py”,第 4061 行,在 init 文件“tkinter_init_.py”中,第 4061 行4006,在 init _tkinter.TclError: 无法打开 "C:\Users\MYNAME~1\AppData\Local\Temp_MEI113162\tkfilebrowser\images\file.png": 没有这样的文件或目录
【解决方案2】:

tkfilebrowser 正在使用一些图像,这些图像需要包含在您的软件中才能运行。这是在我的计算机上运行的构建命令:

pyinstaller --onefile --add-data c:\Users\eric\AppData\Local\Programs\Python\Python38\Lib\site-packages\tkfilebrowser\images;tkfilebrowser\images mytest.py

只需将 c:\Users\eric\AppData\Local\Programs\Python\Python38\Lib\site-package 替换为您自己的站点包的路径

【讨论】:

  • 我收到此错误您知道为什么吗?tkfilebrowser\image :无法加载模块“tkfilebrowser”。有关更多信息,请运行“Import-Module tkfilebrowser”。在 line:1 char:120 + ... hon38\site-packages\tkifilebrowser\images\;tkfilebrowser\image try.py + ~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (tkfilebrowser\image:String) [], CommandNotFoundException + FullyQualifiedErrorId : CouldNotAutoLoadModule
  • @Jacob 出于某种原因,在 ; 之前添加了一个 \在粘贴我的构建命令时。尝试从您的手机中删除
猜你喜欢
  • 2020-07-11
  • 2016-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-14
  • 1970-01-01
  • 2019-12-21
相关资源
最近更新 更多