1

I'm writing a program to read comics with a feature to switch between single-page and double-page view. I'm storing this value (and others) inside a config.ini file, like this:

[settings]
double_view = False

And change it trough a button in my main.py file:

def single_double(self):
    if self.double_page_button.isChecked():
        flag = 'True'
    else:
        flag = 'False'
    
    config['settings']['double_view'] = flag
    self.write_config()

My problem is that I also have a module called page_handler.py that needs to constantly check the value of this variable:

config = ConfigParser()
config.read('config.ini')
double_view = config['settings'].getboolean('double_view')

class MasterPageHandler(SinglePageHandler, DoublePageHandler):
    
    def get_next_page(self):
        if not double_view:
            return SinglePageHandler.get_next_page()
        else:
            return DoublePageHandler.get_next_page()

But it seems to only read the config file once when the program starts, so I would have to read it again inside every function that needs to make the same check. This doesn't feel right though, since that would be a lot of reading.

Is that the way to do it? Maybe I am misunderstanding the use of .ini files?

Eaguilar
  • 19
  • 2
  • 1
    Your main application has access to your instance of ``MasterPageHandler``, right? Then you should make ``double_view`` an instance attribute and whenever ``single_double`` is executed, you set the ``master_page_handler_instace.double_view`` flag accordingly. Side note: if you're reading your ini file in each of your modules, **that** might be a bad design decision. Read the ini only once in your main app and pass either the whole config object around or just the required values (e.g. create MasterPageHandler() with the initial ``double_view`` from the ini) – Mike Scotty Oct 20 '21 at 07:39
  • That's the solution I needed. Thanks a lot. – Eaguilar Oct 20 '21 at 08:23

0 Answers0