Finding Components on Other Windows


Learn how to grab information from components on any open window from a component's event script.

Video recorded using: Ignition 7.7


(open in window)

[00:00] In this video we're going to cover how to write component event scripts that can access the properties from components on other windows. This is something that's a little bit harder to do because you can't bind directly to other windows. After all, you don't know whether or not the other window's going to be open or not when your script runs. So we're going to have to handle that. So we have our main window open here and we're just going to go ahead and put a button on it. And then we're going to make a new window and let's give it a different name. Let's call it OtherWindow. And we'll put a text field on OtherWindow and let's type some text in the text field. All right, so our goal here is to get this button to read the text from this text field. So the first thing we're going to need to do is grab OtherWindow itself from buttons action performed event. And the way to do this is through a system call. So you say system.gui for graphical user interface . , okay, there's a number of ways to do this depending on how your application needs to work, but the most direct way to do it is simply by calling getWindow, and you give it a name. Now, the trick here is that you have to handle the case where a window isn't open. So we're going to say getWindow, and we're going to look for OtherWindow. Okay. Let's assign that to a variable and print it out and see what we have. And you can see it works. We got the other window because of course it's open. Now if that other window were not open, the script would actually throw an error. The call to system.gui, that getWindow throws a value error. And that's not something you want happening in a production system. So to do this a little bit better, we're going to use Python's exception handling. So we say try, and then we open up a new block, and then we're going to catch the value error. All right, and in the case where the value error is raised, we're just going to print out OtherWindow is not open. All right, let's try this out. All right, that's a lot nicer. So we've correctly handled the case where OtherWindow is closed, but if OtherWindow is open, it works just fine and we grab the window. Now that we reliably and safely have figured out how to grab the other window in case it's open, we need to go ahead and drill down and grab the text field's text property. One thing to realize is that when you get the window itself, the first thing you need to do to start walking down into the component hierarchy is grab the root container. And that's done simply by accessing the window's root container property. So we can just say text=window.rootcontainer .getcomponent .text and let's go ahead and pop up this text in a message box. So system.gui.messageBox, okay, the second argument to messageBox is just the title for the message pop up. This is just to give us a little bit of a break from printing things to the console here. Show something a little different. And ta-da, from a button on main window we have popped up a message box with the text from the text field on a different window.

You are editing this transcript.

Make any corrections to improve this transcript. We'll review any changes before posting them.