check if a child frame exists?

917 views
Skip to first unread message

C M

unread,
Jul 10, 2009, 2:07:52 AM7/10/09
to wxPytho...@lists.wxwidgets.org
I have a main frame that can launch another frame, its child.
I don't want to launch a second instance of it if it has already
been launched, so I need to know if it already exists.

So, what's the best way to check to see if a child exists?
I would think I would use .GetChildren(), but I am not sure
how to do something like

children = mainframe.GetChildren()
if childframe in children:
   pass   #don't launch it again.

because I don't know how to match the childframe to
the list returned from GetChildren().

Thanks,
Che

Cody Precord

unread,
Jul 10, 2009, 7:34:49 AM7/10/09
to wxPytho...@googlegroups.com
Hello,

The above code will work fine if the 'childframe' variable is an
instantiated instance of the object and is a child of the 'mainframe'.

2) Another way is if the child frame is a unique subclass that will
only have on instance at a time,

def OnLaunchChild(self,evt):
for child in frame.GetChildren():
if isinstance(child, MyChildFrameClass):
child.Raise()
return
else:
new_child = MyChildFrameClass(frame)
new_child.Show()

3) And another way would be to make the child frame class a singleton
class so that it is only possible to instantiate a single instance of
it at a time.

4) Yet another way is to keep reference to the instance of the child
frame.

def OnLaunchChild(self,evt):
if isinstance(self.child, wx.Frame): # check for dead objects
self.child.Raise()
else:
self.child = MyChildFrameClass(self)
self.child.Show()

Cody

Ross

unread,
Jul 10, 2009, 10:56:14 AM7/10/09
to wxPytho...@googlegroups.com, wxPytho...@lists.wxwidgets.org

There's also the hasattr() function that I've recently learned about
thru this forum if you create the child frame with a specific name

self.new_child = MyChildFrameClass(frame)

I'd think you could simply test to see if you've created that yet:
if hasattr(self, "new_child")

... or something along those lines. Don't quote me on my syntax as
I'm a novice but I've been using 'hasattr' successfully in such
circumstances.

- Ross.

Hong Yuan

unread,
Jul 10, 2009, 2:28:15 AM7/10/09
to wxPython-users
You can put any attribute on your child frame as you like to
distinguish them from each other. For example if you app opens
multiple documents, each in a child frame. Then simply put a filename
attribute on the frame. And if the same file is to be open again,
iterate through your child frames and see if any frame with the same
name is already there.

In a word, how to identify and keep track of the child frames is your
application's work, not the work of the wx framework.

C M

unread,
Jul 10, 2009, 8:41:26 PM7/10/09
to wxPytho...@googlegroups.com

Thank you, Cody, for all those suggestions!  Very helpful!
 
Che

C M

unread,
Jul 10, 2009, 8:42:25 PM7/10/09
to wxPytho...@googlegroups.com

That does seem simpler than I'd thought, thank you!

Che

C M

unread,
Jul 10, 2009, 8:43:20 PM7/10/09
to wxPytho...@googlegroups.com
On Fri, Jul 10, 2009 at 10:56 AM, Ross <ros...@gmail.com> wrote:


There's also the hasattr() function that I've recently learned about
thru this forum  if you create the child frame with a specific name

               self.new_child = MyChildFrameClass(frame)

I'd think you could simply test to see if you've created that yet:
              if hasattr(self, "new_child")

... or something along those lines.  Don't quote me on my syntax as
I'm a novice but I've been using 'hasattr' successfully in such
circumstances.

Yes, I had wondered if hasattr would help here, but I had not yet used
it; it will work for any objects then, I guess.  Good idea.  Thanks.

Reply all
Reply to author
Forward
0 new messages