简体   繁体   中英

Overwrite yaml config file in GUI (python)

I have implemented a GUI interface using PyQt4. In my GUI interface I have a configuration tab where users can make changes to the config file (with line edits). I am able to overwrite the yaml config file in the GUI with these inputted values (I have buttons for load, save, and overwrite), but when these variables are actually being used in other modules, it reads from the original config values. This is how I am reading my config file in the modules that use the variables:

with open("config.yaml", "r") as f:
    config = yaml.safe_load(f)

MIN_VOLTAGE = config['test1']['minVolt']
MAX_VOLTAGE = config['test1']['maxVolt']
MAX_CURR = config['test1']['maxCurr']

My config file looks like this:

test1:
  maxCurr: 5
  maxVolt: 5
  minVolt: -5
test2:
  maxVolt: 8
  setCurr: 3

How will I be able to use the new config values without exiting out of the GUI?

You probably do not update the values MIN_VOLTAGE . MAX_VOLTAGE and MAX_CURR after writing the YAML file, but you do not show enough code to identify the problem.

You code loads the YAML file once (probably at application startup), and the values are populated thereafter. If you overwrite your YAML file, the values in your application will not automatically change unless you either manually modify them or reload the YAML file into those values.

A sane approach would be to store the configuration values in a class which has a save and load method, then use the values from there and also update them there when the user changes configuration. Invoke save after modification, and load at application startup.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM