import os
import json
from tkinter import *
# Functions
def load_config():
config_path = os.path.join(os.getcwd(), "configs", "pref.json")
if os.path.exists(config_path):
with open(config_path, "r") as jsonfile:
config = json.load(jsonfile)
print("Loaded configuration from pref.json")
else:
config = {"silent": False, "theme": ""}
write_config()
return config
def save_config(silent_value, theme_value):
config["silent"] = silent_value
config["theme"] = theme_value
write_config()
def write_config():
config_path = os.path.join(os.getcwd(), "configs", "pref.json")
os.makedirs(os.path.dirname(config_path), exist_ok=True)
with open(config_path, "w") as jsonfile:
json.dump(config, jsonfile)
print("Updated configuration written to pref.json")
def on_silent_change():
save_config(silent.get(), theme.get())
def APP():
app = Tk()
global silent
global theme
global config
config = load_config()
silent = BooleanVar(value=config["silent"])
theme = StringVar(value=config["theme"])
silent_check = Checkbutton(app, text="Silent", variable=silent, command=on_silent_change)
silent_check.pack()
theme_check = Entry(app, textvariable=theme)
theme_check.pack()
app.mainloop()
if __name__ == '__main__':
APP()
In this modified version, the code creates an on_silent_change() function, which is called when the "Silent" checkbox is toggled, and saves the current state of silent and theme to the configuration file. The configuration is loaded upon starting the application, and if no configuration exists, a default configuration is created.