wxPython: Ensure only one instance of a panel is open
Question: I'm writing a multi-window, multi-frame application. For every new
window/frame that is opened, there should be one and only one instance of that
window. I want the user to be able to quickly switch between these windows, so
`ShowModal()` does not work. I've tried using `SingleInstanceChecker`, but I
can't get it to work as it's more for Apps than for frames. How should I
accomplish this?


Answer: I did a bit of Google-Fu and found this old thread:

  * <https://groups.google.com/forum/#!topic/wxpython-users/VTPpXYZYHmM>

Using that as my template, I put together this little script and it appears to
work on my Linux box:

    
    
    import wx
    
    ########################################################################
    class MyPanel(wx.Panel):
        """"""
    
        #----------------------------------------------------------------------
        def __init__(self, parent):
            """Constructor"""
            wx.Panel.__init__(self, parent)
    
    
    ########################################################################
    class SingleInstanceFrame(wx.Frame):
        """"""
    
        instance = None
        init = 0
    
        #----------------------------------------------------------------------
        def __new__(self, *args, **kwargs):
            """"""
            if self.instance is None:
                self.instance = wx.Frame.__new__(self)
            elif isinstance(self.instance, wx._core._wxPyDeadObject):
                self.instance = wx.Frame.__new__(self)
            return self.instance
    
        #----------------------------------------------------------------------
        def __init__(self):
            """Constructor"""
            print id(self)
            if self.init:
                return
            self.init = 1
    
            wx.Frame.__init__(self, None, title="Single Instance Frame")
            panel = MyPanel(self)
            self.Show()
    
    
    
    ########################################################################
    class MainFrame(wx.Frame):
        """"""
    
        #----------------------------------------------------------------------
        def __init__(self):
            """Constructor"""
            wx.Frame.__init__(self, None, title="Main Frame")
            panel = MyPanel(self)
            btn = wx.Button(panel, label="Open Frame")
            btn.Bind(wx.EVT_BUTTON, self.open_frame)
            self.Show()
    
        #----------------------------------------------------------------------
        def open_frame(self, event):
            frame = SingleInstanceFrame()
    
    if __name__ == '__main__':
        app = wx.App(False)
        frame = MainFrame()
        app.MainLoop()
    

