Jump to content
Sign in to follow this  
asnowcappedromance

Pyside - Prevent multiple instances of application from running

Recommended Posts

Hi guys,

 

This has boggled my mind for a while now. I have a OTL that has a tool script attached to it (Type Properties > Tool section), that evokes a PySide interface. The problem is that when I run the tool a few times, every single time a new instance of the interface is being opened, while the old one is still alive. So I'm ending up with lots of open windows of the same tool after a while which is super annoying.

How can I prevent this from happening?

I want to have only one single instance running at a time, basically close the old window if the tool is run again.

 

Here's a really simple example that we could use as a test case. (Same problem if I run it as a shelf tool)

from PySide import QtGui, QtCore

class Window(QtGui.QWidget):
    """Simple Test"""
    
    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(50, 50, 500, 300)
        self.setWindowTitle("test")
        self.home()
        
    def home(self):

        btn = QtGui.QPushButton("Test Button")
        a_label = QtGui.QLabel("Test Label")
        
        qvbox = QtGui.QVBoxLayout()
        qvbox.addWidget(btn)
        qvbox.addWidget(a_label)
        qvbox.insertStretch(2)
        self.setLayout(qvbox)
                    
    def run(self):
        self.show()

main = Window()
main.run()

Some where I read that using the QEventLoop might help to detect running instances with hou.ui.eventLoopCallbacks(),

but after trying some stuff for a few hours, I'm at my wits end.

Help here would be much appreciated :)

 

Thanks!!!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×