Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQt4 : check the window is existing or not

I made a kind of tool for MAYA. Once I call the class and make a instance, I don't have to call it anymore. Instead I must check the window is existing or not. In fact, when I press a button to call close() or "X" button, it doesn't call __del()__ method. I can't clear up my works.

So, I plan to check the instance is existing, and if it is, I don't call class, just call show(). But, I can't find the way.

_win = RigControlWindow()
_win.show()

How can RigControlWindow class find the instance is existing?

like image 421
Hyun-geun Kim Avatar asked Jan 23 '26 01:01

Hyun-geun Kim


2 Answers

Keep a reference to the RigControlWindow instance as a private attribute of the main window.

class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self._rcwin = None

    def showRigControlWindow(self):
        if self._rcwin is None:
            self._rcwin = RigControlWindow()
        self._rcwin.show()

Alternatively, you could use a property:

class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self._rcwin = None

    @property    
    def rcwin(self):
        if self._rcwin is None:
            self._rcwin = RigControlWindow()
        return self._rcwin

    def showRigControlWindow(self):
        self.rcwin.show()
like image 132
ekhumoro Avatar answered Jan 25 '26 06:01

ekhumoro


An alternative to ekhumoro's answer, is to have a function in a module like this:

def startGui():
    if 'myWindows' in globals():
        global myWindows
        myWindows.show()
    else:
        global myWindows
        myWindows = init_gui.MainWindow(parent=init_gui.MyMainWindow())
        myWindows.show()

And then call startGui from a shelf script like this:

if __name__ == '__main__':
    startGui()
like image 43
Purrrple Avatar answered Jan 25 '26 08:01

Purrrple



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!