Python Forum
Storing and using UI variables values across files
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Storing and using UI variables values across files
#1
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:
  • (changed) checked 'Rotate'
  • (changed) set all the Max X, Y, Z values to 2
  • Threshold is 5
  • Using Duplicate
And in the Sub UI, if I have:
  • (changed) Checked 'Preserve Input Connections
  • (changed) Checked 'Activate Groupings', using 'Combine into one Group' option

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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Mar-24-2017, 09:53 PM)ichabod801 Wrote: 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.

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.options
While 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?
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(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.

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.

hmm, sorry I do not really catch what you have been saying..   Huh and pardon my lack of understanding as I am not very well versed in python yet.

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?
Reply
#6
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)
Output:
>>> main = Main() >>> sub = main.sub >>> print(sub.options) {'spam': 'spam', 'eggs': 'spam', 'ham': 'spam'}
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Creating a loop with dynamic variables instead of hardcoded values FugaziRocks 3 1,477 Jul-27-2022, 08:50 PM
Last Post: rob101
  using variables with functions imported from different files. Scordomaniac 3 1,264 May-24-2022, 10:53 AM
Last Post: deanhystad
  Create array of values from 2 variables paulo79 1 1,080 Apr-19-2022, 08:28 PM
Last Post: deanhystad
  Storing variables into one file for use in multiple Jupyter notebooks devansing 1 1,731 Feb-05-2022, 10:04 AM
Last Post: ibreeden
  Storing whole functions in variables dedesssse 3 2,086 Jul-29-2021, 09:17 PM
Last Post: deanhystad
  Beginner question - storing values cybertron2 4 3,185 Mar-09-2021, 04:21 AM
Last Post: deanhystad
  variables vcnt, ocnt, and mcnt adding previous values and not resetting to 0 archanut 2 1,922 Feb-12-2021, 06:56 PM
Last Post: deanhystad
  Giving all possible values to four different variables quest_ 7 2,941 Jan-18-2021, 05:18 AM
Last Post: deanhystad
  How do use data from csv files as variables? JUSS1K 1 2,134 Oct-25-2020, 08:31 PM
Last Post: GOTO10
  Passing Variables between files. victorTJ 3 2,241 Oct-17-2020, 01:45 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020