Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8210513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:05:38+00:00 2026-06-07T10:05:38+00:00

Μy Mainclass creates a simple QmainWindows like this: class mcManageUiC(QtGui.QMainWindow): def __init__(self): super(mcManageUiC, self).__init__()

  • 0

Μy Mainclass creates a simple QmainWindows like this:

class mcManageUiC(QtGui.QMainWindow):
    def __init__(self):
        super(mcManageUiC, self).__init__()

        self.initUI()

    def initUI(self):
        self.show()

And at the end of my file I launch it like this:

def main():
    app = QtGui.QApplication(sys.argv)
    renderManagerVar = mcManageUiC()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

My problem is that each time i source it, it launches a new window.
I would like to know if there is a way to detect existence of previous class instance in my script (so that I close the old one or avoid launching a new one), or any other solutions?

Also, when compiling my code with py2exe, same problem with my .exe file on Windows; it launchs a new window every time. Could i add something in the setup.py for Windows to not act like this?

Is it possible, if yes then how?

Note: I’m using Windows 7 64bit compiling with eclipse.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T10:05:40+00:00Added an answer on June 7, 2026 at 10:05 am

    There are a couple ways to do this, you can use a Class attribute to store all the instances — If you do it this way, you may want to store them as weak references via the weakref module to prevent issues with garbage collecting:

    class MyClass(object):
        _instances=[]
        def __init__(self):
            if(len(self._instances) > 2):
                self._instances.pop(0).kill() #kill the oldest instance
            self._instances.append(self)
    
        def kill(self):
            pass #Do something to kill the instance
    

    This is a little ugly though. You might also want to consider using some sort of Factory which (conditionally) creates a new instance. This method is a little more general.

    import weakref
    class Factory(object):
         def __init__(self,cls,nallowed):
             self.product_class=cls  #What class this Factory produces
             self.nallowed=nallowed  #Number of instances allowed
             self.products=[]
    
         def __call__(self,*args,**kwargs):
             self.products=[x for x in self.products if x() is not None] #filter out dead objects
             if(len(self.products) <= self.nallowed):
                 newproduct=self.product_class(*args,**kwargs)
                 self.products.append(weakref.ref(newproduct))
                 return newproduct
             else:
                 return None
    
    #This factory will create up to 2 instances of MyClass
    #and refuse to create more until at least one of those 
    #instances have died.
    factory=Factory(MyClass,2)   
    i1=factory("foo","bar")      #instance of MyClass
    i2=factory("bar","baz")      #instance of MyClass
    i3=factory("baz","chicken")  #None
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it OK to reference this when initializing a field? public class MainClass {
I created this simple example because I was using a more complex class, a
I try to load the customview class that is inside my mainclass in xml.
Let's assume I have the following code: public class MainClass { public static void
Is it possible to access a parent member in a child class... class MainClass
My main class displays a JMenuBar. This JMenuBar is managed from calculator.ui.MenuBar. public JMenuBar
I am trying to build a simple java program which creates a db file,
I'll try to keep my question simple: I have the following instance: MainClass mc
an anyone look at this simple code (?) and tell me what's wrong please?
I need something simple, an ability to switch views in my app like I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.