Trying to loop open windows and close all except two

Hello all,

So I have a home page. And I have a logo(label atop), which should redirect you to the home page. I need to make sure when that happens I close anything open but my nav and home page.

My current code closes ALL windows. Not sure what I did wrong, I just know its something simple, i’m overthinkingor did wrong. lol

Code is below:

windows = system.gui.getOpenedWindowNames()
for path in windows:
	if path != 'Home' or path != 'Navigation Menu':

Swap that or for an and

1 Like

@josborn thank you! I just facepalmed so hard right now.

1 Like

heh, no worries. I cant tell you how many times missed this type of thing.

Could be:
if path not in {"Home", "Navigation Menu"}:
“Reads” a little bit cleaner, no need to think about the boolean condition, and easily extensible if you want to allow a third window.

I might even go so far as to make an windowsToKeep = {} or allowedWindows or something variable, just to further lower cognitive load the next time you need to scan this in a year or five.


Even better! Can’t say no to clean code. I will freshen it up tomorrow.

Thank you