Jul-29-2018, 11:00 PM
Hey all,
today I somehow managed to get stuck with getting a variable in one module changed by a function in another module - something I had working last time I wrote multi-module scripts.
Simplifying the code (otherwise requiring Kivy to run), I have:
main.py:
First on application startup, a configuration text file is to be read and values in parameters dictionary overwritten with those found in file.
However, the values in dictionary are not affected by read_config_file() function in utils.py. The values from file are read correctly though.
What am I missing? Is there some variable/module naming conflict I overlooked? Am I doing the imports incorrectly?
Thanks for hints,
JC
today I somehow managed to get stuck with getting a variable in one module changed by a function in another module - something I had working last time I wrote multi-module scripts.
Simplifying the code (otherwise requiring Kivy to run), I have:
main.py:
from kivy.app import App import parameters import utils class MainApp() def on_start(self): # runs when application starts utils.read_config_file() # reads config text file and should modify parameters dictionary accordingly Window.clearcolor = hex2color(parameters.parameters["background_color"]) # should use new color value instead of default one if __name__ == '__main__': MainApp().run()parameters.py:
# dictionary with default values parameters= { "config_file": "config.txt", "width": "800", "height": "400", "background_color": "#FFFFFF", }utils.py
import parameters # reads config.txt file and should overwrite default values in parameters dictionary with those found in file def read_config_file(): with open(parameters.parameters["config_file"], "r") as config_file: for line in config_file: if line == "\n" or line == "": continue key, value = line.strip().split() parameters.parameters[key[:-1]] = value.strip()I would like the parameters dictionary (in parameters.py module) to be referenced from other modules, so the variables can be shared.
First on application startup, a configuration text file is to be read and values in parameters dictionary overwritten with those found in file.
However, the values in dictionary are not affected by read_config_file() function in utils.py. The values from file are read correctly though.
What am I missing? Is there some variable/module naming conflict I overlooked? Am I doing the imports incorrectly?
Thanks for hints,
JC