Using singleton to have a single instance of a modeless dialog

Hello everybody,

I have an application from which I would like to launch a modeless
dialog. However, I would like to have only one instance of such a
dialog opened. Even if Singleton design pattern has been criticized for
a while it seems to me that it is relevant in such case.

I implemented this in the little script enclosed. The first time I
launch my dialog, it pops up but the second time I launch it I get the
following error:

test2.py (1.41 KB)

···
--
Eric Pellegrini
Calcul Scientifique
Institut Laue-Langevin
Grenoble, France

Pellegrini Eric wrote:

Hello everybody,

      I have an application from which I would like to launch a

modeless
dialog. However, I would like to have only one instance of
such a
dialog opened. Even if Singleton design pattern has been
criticized for
a while it seems to me that it is relevant in such case.

      I implemented this in the little script enclosed. The first

time I
launch my dialog, it pops up but the second time I launch it I
get the
following error:

You're getting a call to __new__ and a call to __init__ each time. 

The second call to init fails, because the wx.Frame has already
been initialized. I made this modification:

`class ColorEditor(wx.Frame):

     

      instance = None

      init = 0

     

      def __new__(self, *args, **kwargs):

          print "ColorEditor.__new__"

          if self.instance is None:

              self.instance = wx.Frame.__new__(self)

          return self.instance

             

      def __init__(self, parent, *args, **kwargs):

          print "ColorEditor.__init__", id(self)

          if self.init:

              return

          self.init = 1

          ...`
···
-- Tim Roberts, Providenza & Boekelheide, Inc.

timr@probo.com

Thanks for the hint Tim. That’s OK now

Eric

···

De : Tim Roberts timr@probo.com
À :wxpython-users@googlegroups.comwxpython-users@googlegroups.com
Envoyé le : Mardi 30 Août 2011 19h47
Objet : Re: [wxPython-users] Using singleton to have a single instance of a modeless dialog

Pellegrini Eric wrote:

Hello everybody,

      I have an application from which I would like to launch a

modeless
dialog. However, I would like to have only one instance of
such a
dialog opened. Even if Singleton design pattern has been
criticized for
a while it seems to me that it is relevant in such case.

      I implemented this in the little script enclosed. The first

time I
launch my dialog, it pops up but the second time I launch it I
get the
following error:

You're getting a call to __new__ and a call to __init__ each time. 

The second call to init fails, because the wx.Frame has already
been initialized. I made this modification:

`class ColorEditor(wx.Frame):

     

      instance = None

      init = 0

     

      def __new__(self, *args, **kwargs):

          print "ColorEditor.__new__"

          if self.instance is None:

              self.instance = wx.Frame.__new__(self)

          return self.instance

             

      def __init__(self, parent, *args, **kwargs):

          print "ColorEditor.__init__", id(self)

          if self.init:

              return

          self.init = 1

          ...`
-- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en