Open/close docked window for certain main windows

I have a docked window that is only for certain main windows (Main Window 1,2, and 3). On startup, Vision Client opens up to Main Window 1 with the docked window. I want to close the docked window when the user opens a different window of Vision Client, or none of the main windows of interest are open. However, even when another main window is open, the docked window remains. Therefore, I added this component scripting under the docked window's visionWindowOpened.

main_window_names = ["Main Window 1",
					"Main Window 2",
					"Main Windows 3",

def are_main_windows_open():
	for window_name in main_window_names:
		window = system.gui.getWindow(window_name)
		if window is None:
			return False
	return True
	
def close_docked_if_needed():
	docked_window = system.gui.getWindow("Docked Window")
	if docked_window is not None:
		if not are_main_windows_open():
			docked_window.close()

close_docked_if_needed()

However, I'm getting a ValueError that "Main Window 2 is not currently open." I'm not sure if my code is incorrect or if I shouldn't script this under the docked window's event handler.

Is there anybody that can help on this?

Can't fully answer the main topic, but you're getting a "ValueError" because you are trying to get a window that is not opened.

As a workaround, consider catching the error by doing the below (this comes straight from Ignition's documentation) :

# This example will get the window named 'Overview' and then close it.
  
try:
   window = system.gui.getWindow('Overview')
   system.gui.closeWindow(window)
  
except ValueError:
   system.gui.warningBox("The Overview window isn't open")

Instead of the system.gui.warningBox in the except, consider using pass if you do not want to do anything with the error.

1 Like

Additionally, the try/except with gui locking popup could be avoided by qualifying the action in this way:

# If the overview window is open, close it
if 'Overview' in system.gui.getOpenedWindowNames():
	window = system.gui.getWindow('Overview')
	system.gui.closeWindow(window)