mGui Window testing from within a class

I’m adapting some UI classes to mGui -which is wonderful - and I’m bumping up against the old “how do I test if the window exists?” issue.
The mGui docs have this idiom:

if my_window:
    my_window.show()
else:
    my_window = Window(....)
    my_window.show()

but how does this work when my_window is scoped into a class?

import pymel.core as pm
from mGui.gui import *
from mGui.forms import *

class UI(object):

    def __init__(self, data=None):
        self.window = None

    def create_window(self):
        # create
        with Window('UI') as window:
            with FooterForm() as main:
                with VerticalForm() as form:
                    text = Text("hello")
                with HorizontalStretchForm() as footer:
                    btn_1 = Button('command')

        window.main.footer.btn_1.command += self.test
        return window

    def test(self, *args, **kwargs):
        print "{}, {}".format(args,kwargs)

    def run(self):
    
    	# is pm.window the best way here?
        if pm.window('UI',query=True, exists=True):
            pm.deleteUI('UI')

        if not self.window:
            self.window = self.create_window()
        self.window.show()

ui=UI()
ui.run()

The pm.window('UI',query=True, exists=True): test feels clunky, but the mGui Window() class doesn’t seem to work like that. Does mGui have a more elegant solution? Is there a better approach?

Couldn’t you just do:

if self.window:
    cmds.deleteUI(self.window)
self._window = self.create_window()

I think that is vaguely how I handled it in the past. I’d need to go digging through some of my code at home to be sure though.

that did not seem to work for me.
It causes the RuntimeError Object's name 'UI' is not unique. # if you run() with the window already created.

the class instance doesn’t seem to know about windows outside of itself, without querying via pm.window()

Oh yeah good catch.
Also I totally forgot to look into how I was handling this. Hopefully I remember tonight.
Now that I’m actually thinking about it.
Odds are good that I probably set the name somewhere and did an cmds.window(self._name, exist=True) check.

I might have gotten crazy and did if Window.wrap('UI'): check to stay more in the universe of mGui.

I generally don’t bother with naming windows at all, that way I don’t have to worry about accidental name collisions. What happens if you just don’t name the window explictly?

My UI scripts typically get called this way:

ui = UI()
ui.run()

Without the name & pm.window() query, I get multiple windows as I test.
Makes sense, as I’m creating a new instance of the class each time,
and one instance won’t know about another’s .window attribute.

Instead of looking inside my window class for a solutions,
What’s needed is to test if an instance of the UI Class itself already exists:

if not ui:
    ui = UI()
ui.run()

And then all work as advertised. Pretty Obvious, just needed to think it through.

PS mGui is wonderful, thanks to both of you for your efforts

You could tweak the truth-test of the UI class:

     class UI(Object):
            
            ....

           def __nonzero__(self):
                 return bool(self.window)