Top | Previous | Next |
Working with Components |
When you're writing component event handlers, you'll do a lot of work with components. You'll need to reference various components on the window or on other windows, you'll need to reference and set properties of the component, you may even want to move components around on the screen. Finding Components When you have an event object, that object becomes your window into the entire component hierarchy. event.source references the component that fired whatever event you're responding to. event.source.parent references the container that component is in. event.source.parent.getComponent("Name") finds a sibling component with a certain name. The manual page for the event object covers this topic in more detail. Accessing Component Properties Once you have a reference to a component, you can treat it just like any Python object. You can call functions on it, and you can reference its properties, both standard and dynamic, with the "." accessor. For example, you could put this in a button next to the table, which would tell the user which row was selected, then clear the selection, and then print the table.
table = event.source.parent.getComponent("Table")
# Referencing properties of a component row = table.selectedRow system.gui.messageBox("The selected row is : %d" % row)
# Setting properties of a component. table.selectedRow = -1
# Calling functions on components table.print()
Finding Components on Other Windows Sometimes you may want to reference components on other windows. Or maybe you don't have an 'event' object because you're writing a project event script. In this case, you'll need to look up the containing window first. You can do this with the system.gui.getWindow function. This function will throw a ValueError if the given window isn't open, so you should make sure your code handles that gracefully. Once you have a Window, you can use its rootContainer property to get into the standard component hierarchy. This code will look up the HeaderLabel on a window named "Overview" and set its text and foreground color.
try: window = system.gui.getWindow("Overview") label = window.rootContainer.getComponent("HeaderLabel") label.text = "Notice Me!" label.foreground = (255,0,0) except ValueError: # ignore error with a pass keyword pass
Common Component Functions There are a number of functions that are common to all components in Ignition.
Moving/Resizing Components and Windows You can use scripting to move and resize a component at runtime. The functions system.gui.moveComponent, system.gui.reshapeComponent and system.gui.resizeComponent are used for this. They simply take a component, and a new size, location, or both. Locations are always measured in pixels from the upper left point of the component's parent container.
Note that if you're moving a component that is set to relative layout, your coordinates will act as if they were coordinates to the sizes of the relevant containers last time they were saved in the Designer, not the current real coordinates of the runtime. This is very helpful for creating animations. In effect what this means is that the coordinates fed into these functions "respect" the relative layout system automatically.
You can move and resize windows as well. If you have a reference to a window, you can set its size and location directly. For example, if you wanted to move the floating window Popup3 to certain location, you could do so like this:
try: window = system.gui.getWindow("Popup3") window.setSize(250,600) window.setLocation(0,0) except ValueError: # ignore error with a pass keyword pass
See also: |