[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] Functions in config files



Hi,
 
In my GUI toolkit, I have config files that the program can parse and turn into a complete menu screen. Now, if I have a button in my menu, and I want that button to run a function when it is clicked, then I need some way of naming a function from the config file.
 
At the moment I am just parsing the function name and running it through eval() so it is translated into a real function. For example, a button may have it's function set to "exit", which evaluates into Python's exit() function when parsed.
 
This has always felt a bit awkward to me, and now it seems that this method doesn't work when a project is compiled with PyInstaller. So, I was wondering if there was a well known solution to this problem?
 
The first thing I've thought of is accessing it through a dictionary. So, if I instance a Menu class, and then give it a dictionary that uses a string as a key, and the approriate function as a value. Then I can just index this dictionary with the string parsed from the config file, in order to return a function.
 
If anybody can provide any feedback on this, or any better methods for achieving this, it would be greatly appreciated.
 
Thanks,
Sam Bull