pyinstaller添加可编辑文件



我创建了一个简单的Tkinter应用程序,该应用程序可以创建ssh隧道,
保存用户输入以供下次启动该应用程序的最佳方式是什么

我试图创建一个JSON/pickle文件来保存数据,
我在关闭前打开了文件,发现它发生了变化,
然而,当重新启动应用程序时,文件内容恢复到修改前的状态

install.bat

..venvScriptspyinstaller.exe ^
--clean ^
--onefile ^
--name %OUTPUT_NAME% ^
--add-data default_values.json;.^
--win-private-assemblies ^
.main.py

我试图在没有任何帮助的情况下添加带有标志的游戏,例如--noupx--debug all

第一个应用程序打开:

on app start
{
"ssh_address_or_host": "192.168.1.1",
"ssh_username": "username",
"ssh_password": "password",
"local_bind_address_address": "localhost", 
"local_bind_address_port": 5902,
"remote_bind_address_address": "localhost",
"remote_bind_address_port": 5902
}
before app close:
{
"ssh_address_or_host": "192.168.1.100",   
"ssh_username": "username",
"ssh_password": "password",
"local_bind_address_address": "localhost", 
"local_bind_address_port": 5902,
"remote_bind_address_address": "localhost",
"remote_bind_address_port": 5902
}
elapsed: 0.46

第二个应用程序打开:

on app start
{
"ssh_address_or_host": "192.168.1.1",
"ssh_username": "username",
"ssh_password": "password",
"local_bind_address_address": "localhost",
"local_bind_address_port": 5902,
"remote_bind_address_address": "localhost",
"remote_bind_address_port": 5902
}

我能做什么?

PyInstaller的onefile选项通过将Python解释器、脚本和依赖项打包到一个可执行文件中来工作,该可执行文件在执行时会被解压缩到一个新的临时目录中。下次启动应用程序时,写入该目录的文件将不存在,因为所有文件都将被解压缩到不同的临时目录中。

您应该将设置文件读/写到应用程序数据目录:

import os
import pathlib
import json
# e.g. C:UsersOnson SweemeyAppDataRoamingRamis App
settings_dir = pathlib.Path(os.getenv("APPDATA"), "Ramis App")
# e.g. C:UsersOnson SweemeyAppDataRoamingRamis Appsettings.json
settings_filepath = settings_dir.joinpath("settings.json")
# On start-up, load the saved settings if they exist, or use the default settings
try:
with open(settings_filepath) as f:
settings = json.load(f)
except FileNotFoundError:
settings = {
"ssh_address_or_host": "192.168.1.1",
"ssh_username": "username",
"ssh_password": "password",
"local_bind_address_address": "localhost", 
"local_bind_address_port": 5902,
"remote_bind_address_address": "localhost",
"remote_bind_address_port": 5902
}
# On application exit, save the settings
# Create the directory, since it might not exist
settings_dir.mkdir(parents=True, exist_ok=True)
with open(settings_filepath, "w") as f:
json.dump(settings, f)

相关内容

最新更新