python - 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:
- I created the exe file by using:
pyinstaller main_script.py
Then I modify the .spec and I run "pyinstaller main_script.spec"
The main_script.py of the project interconnects with other scripts and reads files from other folders inside the main folder. This seems to work properly when running the .exe file
kivy version: v1.11.1
python version: Python 3.7.4
pyinstaller version: 4.0
matplotlib version: 3.1.3
解决方案
我不是 100% 的,但我在 iOS 上也遇到过类似的问题。诀窍是将您的照片保存在文件系统中,应用程序可以使用该文件系统来存储其他数据。
在 Windows 计算机上,此目录是%APPDATA%/<app_name>
值得庆幸的是,Kivy 背后的大脑创建了 App 类的一个方便属性,该属性返回一个指向正确目录的路径,名为user_data_dir
.
保存照片以App.user_data_dir
解决我的问题。也许它会为你工作?让我知道。
推荐阅读
- c - 从函数调用时 munmap() 不起作用
- ruby - 安装 ruby-2.1.2:无法加载此类文件 -- openssl (LoadError)
- python - PyQt5 fbs 无法执行 slack
- android - 滚动 Android RecyclerView 并控制屏幕上的确切位置
- java - Tim 排序实现在低数组大小下工作,但在高数组大小下崩溃
- outlook - 带有 RTF 的 IConverterSession
- javascript - 仅执行作为字符串注入的部分代码
- java - 如何在openjdk11环境下创建exe文件?
- swift - Ubuntu 上的服务器端 Swift 和 Vapor
- java - Spring Boot appengine 应用程序未从 application.yml 中的 app.yaml 中选择环境变量