check if a child frame exists?

Showing 1-7 of 7 messages
check if a child frame exists? Che M 7/9/09 11:07 PM
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
Re: [wxPython-users] check if a child frame exists? Cody Precord 7/10/09 4:34 AM
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

Re: [wxPython-users] check if a child frame exists? Ross 7/10/09 7:56 AM

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.
Re: check if a child frame exists? Hong Yuan 7/9/09 11:28 PM
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.
Re: [wxPython-users] Re: check if a child frame exists? Che M 7/10/09 5:41 PM



Thank you, Cody, for all those suggestions!  Very helpful!
 
Che
Re: [wxPython-users] Re: check if a child frame exists? Che M 7/10/09 5:42 PM



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

Che

Re: [wxPython-users] Re: check if a child frame exists? Che M 7/10/09 5:43 PM


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.