![]() |
Storing and using UI variables values across files - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Storing and using UI variables values across files (/thread-2559.html) |
Storing and using UI variables values across files - xenas - Mar-24-2017 Hi all, not so sure where I should post my thread.. Anyways I have 2 ui files - main_ui and sub_ui , along with several other files eg. utils.py (that contain common function by other scripts) etc. for manageable purposes.I have made a simple example in which in both of my Uis, I have a variety of values that I will need to use in order to use the tool functionality as defined by main_tool_btn .Some screenshots of the main and sub ui (since I can't post links...): main_UI : http : //imgur.com/a/mcApj sub_UI : http : //imgur.com/a/I7PJP Example In the Main UI, if I have:
How can I/ what is the best way for me to store such changes so that when I hit the Tool Mode in the main UI, it will read in such values?In creation_funcs() , I tried using a global variable, while it can work for a small scale code, but as the example I have given is a very simple form, my UI actually has way more than that in which I don't think it is a viable idea to keep using globals?Thought I had let you guys know in advance that I am not very well-versed in using Python yet, so please pardon my codings should it looks out of place. code: main_ui_code : http : //pastebin.com/raw/99dSQki2 sub_ui_code : http : //pastebin.com/raw/9NveYCJJ RE: Storing and using UI variables values across files - ichabod801 - Mar-24-2017 I usually handle this with an options dict: options = {'rotate': True, 'max_x': 2, 'max_y': 2, 'max_z': 2, 'threshold': 5}This could be an attribute of the main_ui, which could be passed to the sub_ui. Then the sub_ui could update it's own option dict based on the one passed to it by the main_ui. For storage between sessions I just write the key/value pairs to a text file. RE: Storing and using UI variables values across files - xenas - Mar-24-2017 (Mar-24-2017, 09:53 PM)ichabod801 Wrote: I usually handle this with an options dict: Hi ichabod801, thanks for getting back. So if I were to access/ update this dictionary in other files, do I need to keep doing the following: import main_ui options_dict = main_ui.optionsWhile I am not that concern about accessing it, I am mostly worried about the updating part as I have the fear that the values will not be updated accordingly Sorry just read that you mentioned about writing the key/values into a text file. But is it possible not to do this - creating an additional text file solely for this? RE: Storing and using UI variables values across files - ichabod801 - Mar-24-2017 I didn't look at your full code, but I usually do my UIs as a hierarchical set of classes with links to each other. That way they can all access the options through their links to the parent (or master) object. So options wouldn't be a global in the main_ui module, it would be an attribute of the MainUI class in the main_ui module. RE: Storing and using UI variables values across files - xenas - Mar-24-2017 (Mar-24-2017, 10:10 PM)ichabod801 Wrote: I didn't look at your full code, but I usually do my UIs as a hierarchical set of classes with links to each other. That way they can all access the options through their links to the parent (or master) object. hmm, sorry I do not really catch what you have been saying.. ![]() In fact, this is my first time writing something this complicated.. Would you kindly give me an example or any projects etc for me to view? RE: Storing and using UI variables values across files - ichabod801 - Mar-24-2017 class Main(object): def __init__(self): self.options = {'spam': 'spam', 'eggs': 'spam'} self.sub = Sub(self) class Sub(object): def __init__(self, parent): self.parent = parent self.options = {'ham': 'spam'} self.options.update(parent.options) So the parent attribute of the Sub class points to an instance of the Main class (in the output that is main). So the Sub instance can update it's own options with the Main instance's options by using parent.options.I skimmed your code, and it looks like Qt is taking care of that parent attribute for you (I'm not familiar with Qt, however). So you just need to create an options attribute in the MainUI class, and you should be able to access that in the SubUI class by using parent.options. |