from PySide6 import QtWidgets, QtCore, QtGui import os class Attr_Dict (dict) : def __getattr__ (self, key) : return self [key] # end def __getattr__ # end class Attr_Dict class Main_Window (QtWidgets.QMainWindow) : DEFAULT_SETTINGS_FILE = "settings.ini" APP_NAME = "Main Window" VERSION = "0" APP_ORGANISATION = ("Org-Name", "Org-Domain") def __init__ (self, app, settings) : super ().__init__ () self.settings = settings self.setWindowTitle ("%s %s" % (self.APP_NAME, self.VERSION)) self._setupActions (app) self._setupGui () if not self.restore_settings (settings) : self.show () # end def __init__ def _setupActions (self, app) : self.actions = Attr_Dict () self._add_action ("exit", "Exit", self.close, icon = ":/icons/exit.png") # end def _setupActions def _add_action (self, name, label, callback, icon = None, ** options) : A = QtGui.QAction (label, self, ** options) if icon : A.setIcon (QtGui.QIcon (icon)) self.actions [name] = A A.triggered.connect (callback) # end def _add_action def _setupGui (self) : pass # end def _setupGui def closeEvent (self, * args) : self.save_settings () super ().closeEvent (* args) # end def closeEvent @classmethod def _open_settings (cls, settings_file = None) : if not settings_file : settings_file = os.path.join (cls.APP_DIR, cls.DEFAULT_SETTINGS_FILE) QtCore.QCoreApplication.setOrganizationName (cls.APP_ORGANISATION [0]) QtCore.QCoreApplication.setOrganizationDomain (cls.APP_ORGANISATION [1]) QtCore.QCoreApplication.setApplicationName (cls.APP_NAME) return QtCore.QSettings (settings_file, QtCore.QSettings.IniFormat) # end def _open_settings @classmethod def load_settings (cls, cmd) : settings = cls._open_settings (cmd.settings_file) ps = cls._load_app_settings (settings) settings.beginGroup ("TempSettings") settings.setValue ("settingsFile", cmd.settings_file) for n, v in ps.items () : if getattr (cmd, n, None) != None : v = getattr (cmd, n) settings.setValue (n, v) settings.endGroup () return settings # end def load_settings def restore_settings (self, settings) : self.settings = settings QtCore.QTimer.singleShot (1, self._restore_settings) # end def restore_settings def _restore_settings (self) : settings = self.settings settings.beginGroup ("MainWindow") self.restoreGeometry (settings.value ("geometry" )) self.restoreState (settings.value ("windowState")) settings.endGroup () # end def _restore_settings def save_settings (self) : settings = self.settings settings.beginGroup ("MainWindow") settings.setValue ("geometry", self.saveGeometry ()) settings.setValue ("windowState", self.saveState ()) settings.endGroup () # end def save_settings @classmethod def run (cls, settings) : QtWidgets.QApplication.setStyle ("macintosh") app = QtWidgets.QApplication ([]) if cls.pre_main_window_action (settings) : self = cls (app, settings) if self.isVisible () : app.exec_ () # end def run @classmethod def pre_main_window_action (cls, settings) : return True # end def pre_main_window_action # end class Main_Window ### __END__ Main_Window