首页 > 解决方案 > Python GUI using Kivy. Cannot save figures when running .exe file

问题描述

Using Python and Kivy I have created an interface. When I run the code in Pycharm everything goes smoothly, but when I run the .exe file of the application, the code crashes just before saving some figures.

In the code, at some point, I create some figures and I save them in one folder (/Results_plots) inside the main folder where the project is located (/main). The figures are saved using:

plt.savefig(f"Results_plots/Comparison/figure1")

I have also tried without succeeding:

basepath=os.path.abspath(str(f"Results_plots/Comparison"))

plt.figure(os.path.join(basepath, f"figure1"))

Extra information that might help:

pyinstaller main_script.py

Then I modify the .spec and I run "pyinstaller main_script.spec"

标签: pythonuser-interfacekivyexe

解决方案


我不是 100% 的,但我在 iOS 上也遇到过类似的问题。诀窍是将您的照片保存在文件系统中,应用程序可以使用该文件系统来存储其他数据。

在 Windows 计算机上,此目录是%APPDATA%/<app_name>

值得庆幸的是,Kivy 背后的大脑创建了 App 类的一个方便属性,该属性返回一个指向正确目录的路径,名为user_data_dir.

保存照片以App.user_data_dir解决我的问题。也许它会为你工作?让我知道。


推荐阅读