What type of behaviour do you want if someone tries to generate a GUI when one is already running?
Basically what you can do is add a num_instances
attribute to your class which will be incremented each time an instance is created, then decremented in your class's __del__
method. You can then check, either in __init__
or __new__
depending on the behaviour you want.
class GUI:
num_instances = 0
def __init__(self):
if self.__class__.num_instances:
raise
self.__class__.num_instances += 1
def __del__(self):
self.__class__.num_instances -= 1
This is just a draft, so check that it indeed always behave as desired, but it should get you going.
EDIT:
I forgot to mention it but of course you can also look at the singleton pattern
EDIT 2:
for preventing multiple runs, look at this post