How to get kivy to set its defaults in the Config at runtime?

40 Views Asked by At

How can I tell Kivy to update a given Config with its default values at runtime?

I have a "reset settings to defaults" button in my app that deletes all of the entries in my Kivy Config, including in the [kivy] section.

# delete all the options saved to the config file
sections = ['kivy','myapp','etc']
   for section in sections:
      for key in Config[section]:
         Config.remove_option( section, key )

# write-out the changes to the .ini file
Config.write()

After the above commands execute, I'm left with an empty [kivy] section in my .ini file, as desired.

The problem is that I'm having a NoOptionError later in my code that's expecting the default_font option in the Kivy Config

configparser.NoOptionError: No option 'default_font' in section: 'kivy

After the above code wipes away any config options in the [kivy] section of my .ini file, how can I tell Kivy to repopulate it with the defaults?

When I restart the app, Kivy does repopulate the [kivy] section of my .ini file as desired. And I can see that the code that does that is located here:

     elif version == 16:
         Config.setdefault('kivy', 'default_font', [
             'Roboto',
             'data/fonts/Roboto-Regular.ttf',
             'data/fonts/Roboto-Italic.ttf',
             'data/fonts/Roboto-Bold.ttf',
             'data/fonts/Roboto-BoldItalic.ttf'])

But how can I execute the above kivy code at runtime so that it updates my Config with the defaults without having to restart the app?

Note I'm aware that I can manually make my own build_config() function and call Config.setdefaults('kivy', {...}) where I type the default values myself in the dictionary. That's not what I'm asking for. I'm asking how to call the above code that's part of kivy, so the code is not redundant and is future-proof.

1

There are 1 best solutions below

6
John Anderson On

I don't think that changing your Config defaults will have any effect on your currently running App if you change them as you describe. You can force reset by just having your Button remove the default config.ini. The path to the default config.ini is in Config.filename. The next time any kivy app is started, the default will be restored.

Another option is to not use Config.write() at all (that just saves the current settings to the ini file and is not necessary when just changing the settings for the current App). Then just do a Config.read(Config.filename) to restore the settings that are still in the ini file.