Dec-12-2022, 07:01 AM
(This post was last modified: Dec-12-2022, 12:48 PM by Yoriz.
Edit Reason: Added code tags
)
Here's the singleton preferences class below
This is in a .py file by itselfs. I can't load it in one module then use it in all of them because it doesn't exist in any of the other .py files. I could put all of my classes into one big file then use the singleton a single time for use in all classes but I have a hard time finding anything when it's all one big file (that's why includes where invented right?).
This singleton is indeed the same in every instance but I can't seem to figure out a why to load the data from disk just once... Well, I could have the main program call the loading instead of doing it inside the prefs class itself but that is all I can think of.
Edit: Code snippet tag doesn't seem to work on this forum (or I just can't figure it out)
This is in a .py file by itselfs. I can't load it in one module then use it in all of them because it doesn't exist in any of the other .py files. I could put all of my classes into one big file then use the singleton a single time for use in all classes but I have a hard time finding anything when it's all one big file (that's why includes where invented right?).
This singleton is indeed the same in every instance but I can't seem to figure out a why to load the data from disk just once... Well, I could have the main program call the loading instead of doing it inside the prefs class itself but that is all I can think of.
Edit: Code snippet tag doesn't seem to work on this forum (or I just can't figure it out)
#Singleton Preferences from PySide6.QtGui import QColor,QFont from PySide6.QtCore import Qt from os import path from pubsub import pub import json class Preferences(object): def __new__(self): """ creates a singleton object, if it is not created, or else returns the previous singleton object""" if not hasattr(self, 'instance'): self.instance = super(Preferences, self).__new__(self) return self.instance def __init__(self): self._dict = None #<Todo> # Singleton Preferences # figure out how to tell if it already exists so only loads once #</Todo> if path.exists("prefs.json"): self.loadPrefs() else: self.setDefaults() def getFont(self): return QFont(self._dict["fontFamily"],self._dict["fontSize"]) def setFontFamily(self,val): print(val) self._dict["fontFamily"] = val def getFontSize(self): return self._dict["fontSize"] def setFontSize(self,val): self._dict["fontSize"] = val def getFontColor(self): return QColor.fromString(self._dict["fontColor"]) def setFontColor(self,val): self._dict["fontColor"] = val def getButtonWidth(self): return self._dict["buttonWidth"] def getButtonHeight(self): return self._dict["buttonHeight"] def showDict(self): print(self._dict) def setDefaults(self): self._dict = { "fontFamily":"None", "fontSize":16, "fontColor": "white", "buttonWidth":120, "buttonHeight":40 } self.savePrefs() def loadPrefs(self): print("Loading singleton Preferences") with open('prefs.json', 'r') as openfile: self._dict = json.load(openfile) def savePrefs(self): self.showDict() print("Save singleton Prefs") _prefs = json.dumps(self._dict, indent=4) with open("prefs.json", "w") as outfile: outfile.write(_prefs)
Gribouillis write Dec-12-2022, 09:21 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.