Page 1 of 1

PYQt singleton prob

Unread postPosted: Thu Jan 02, 2020 7:12 pm
by guydubost
Hi There
I m building a PyQt app to submit renders on our farm.
I was able to start my PyQt app from a custom menu item.
menu.add_command("RFX>Farm submit", "path to python script")
now if i run my script a second time , a second app Gui pops up and i dont want that
I thought first that i could store a global var and store my GUI in it so second time this var would nt be None .
but seems like python scripts run in a different python session so that global var are just global to the current python script but not shared among different python scripts.

I found a workaround with the linux command wmctrl where i can retrieve the pid of my tool window and check if it match current pid of clarisse , so i know the tool is already running

Do u have soamthing in mind that could avoid the above workaround ?

Re: PYQt singleton prob

Unread postPosted: Fri Jan 03, 2020 10:01 am
by dcourtois

If I understand correctly you just want to prevent your menu item to create the same window multiple times ? In this case, you can get the top level widgets from Qt and check if you have one matching your window. Something like this:

python code

# check for your window by title (or anything else)
found = False
for widget in QApplication.topLevelWidgets():
if widget.windowTitle() == "farm_submitter":
# show the window if it was hidden
if widget.isHidden():
found = True

if found is False:
# create the window

Full reference: ... velWidgets

Re: PYQt singleton prob

Unread postPosted: Fri Jan 03, 2020 6:12 pm
by guydubost
good find. works as well

Re: PYQt singleton prob

Unread postPosted: Tue Jan 07, 2020 4:51 am
by mati
If your global variable is located in another python module found in PYTHONPATH, it should work. It looks like an imported module stays in memory and is not reloaded. Here's an example of a shelf button or menu item script:

Code: Select all
import test_module

test_module.called_count += 1
print("This shelf button has been clicked {} times".format(test_module.called_count))

test_module just contains:
Code: Select all
called_count = 0

I can click on the shelf button or menu item multiple times in a Clarisse session and see that the counter increases.

For Qt, I keep track of the instance on the window class itself and clean it up before launching a new window.

Code: Select all
class MyWindow(QMainWindow):
    _instance = None
    def __init__(self, parent, **kwargs):
        if MyWindow._instance is not None:
            MyWindow._instance = None
        super(MyWindow, self).__init__(parent, **kwargs)