Global Variables

In my HMI, I want the selected row number to be a global variable which changes when the user chooses an entry in a dropdown list.

Here is the code in app.variables:

def getRow_Number():
    global Row_Number
        return Row_Number
    except NameError:
        Row_Number = 0
    return Row_Number

def setRow_Number(newValue):
    global Row_Number
    Row_Number = newValue 

However, I’m not sure how to call getRow_Number() when fetching a SQL query:

FROM Tank_Table
WHERE Row_Number = app.variables.getRow_Number()

I get a gateway error saying that app.variables.getRow_Number() is an invalid object name. Am I doing something wrong syntactically?

Your global functions look good to me. The problem is that you can’t call a Jython function inside of an SQL query/binding. You can plug in the value of an object’s property, including dynamic properties, with the chain link icon. In this case you must use Jython since you’re using a global value because we don’t yet support global dynamic variables (They’ll exist with SQLTags, our next major feature). Here is an example Jython Script to run a query based on a global variable.

I’m assuming ‘Tank’ is an INT. There may be a few changes based on data type.

query =‘SELECT Tank FROM Tank_Table WHERE Row_Number = %d’ % row
tankValue = fpmi.db.runScalarQuery(query)

#now set your property or do what you want with tankValue
#for example:
#event.source.parent.getComponent(‘Numeric Label’).value=tankValue[/code]

Could you better describe what you’re trying to do? There may be an easier way.

Okay, here’s what I’m trying to do:

On a menu screen which is always visible, the user can choose a tank using a dropdown list. The list consists of tank aliases and their correseponding row numbers in Tank_Table.

The program contains several other windows which must use the tank’s row number to display other information fromTank_Table. For instance, my previously shown SQL query bound the tank’s number (not its alias) to that of the currently selected row/alias.

Earlier, in the menu window, I tried to place a container that was the parent of all the other windows, but I couldn’t get it to work.

I’m a bit confused; where should your code go in my program?

Thanks again for your help!

Ok, I think I have an answer for you.

You can use the windows internalFrameOpened event or a refresh button to do your work. Add a dynamic property to your table called myQuery (string), and in the refresh button or internalFrameOpened event, put:

row=app.variables.getRow_Number() {path to the table}.myQuery = 'SELECT Tank FROM Tank_Table WHERE Row_Number = %d' % row
And then in the SQL Query data binding for the table, just link to the myQuery dynamic property you created.

Of course, you don’t need global variables at all. Assuming your Tank Dropdown contains two rows (row_num and alias), you can access the dropdown’s selected value from any window. You can use:

tankNum = fpmi.gui.getWindow('menu_screen').getRootContainer().getComponent('Dropdown_Tank_Select').selectedValue
in the refresh button or internalFrameOpened event for the same result (and then the dropdown doesn’t need to use setRow_Number).

Hope that helps!

This is a case where a global bindable property will make life much easier - and they’re around the corner with SQLTags. Robert has the right idea - here’s how I would expand on it a little.

  1. Create a dynamic property on the root container of every window with the same name. I’ll use tankNum in this example. Use this value in all of your Tank queries in each window. Your queries will be automatically refreshed whenever tankNum changes.

  2. Now we ensure that the correct tank is selected when the user opens the window. As Robert suggested, on the internalFrameOpened event of each applicable window, put a script that sets tankNum to the selected tank. Write this once in a script module and call it from each window in 1 line of code. If you can guarantee that the menu window will be open, simply read the selected value of the dropdown from there. If you cannot, then I’d use a global Jython Variable. If you are using your own navigation script, such as with our Skeleton project, I would place the code there.

  3. Next we propagate dropdown selection changes to each open window that cares. The following script will go through each open window that has tankNum defined and set the value to the dropDown’s selectedValue. Run it on a propertyChange event of the dropdown.

tankNum=fpmi.gui.getWindow('menu_screen').getRootContainer().getComponent('Dropdown_Tank_Select').selectedValue windows = fpmi.gui.getOpenedWindows() for window in windows: try: window.getRootContainer().tankNum=tankNum except AttributeError: pass #property not defined, do nothing

If this is unclear, I can get you through it over Gotomeeting.