Quantcast

is there a method to test if an (wx) object exists ?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

is there a method to test if an (wx) object exists ?

Stef Mientki-2
hi,

Is there a method to test if an (wx) object exists ?

This might a general Python question,
but I couldn't find anything on the regular Python list,
and I guess this might be a regular problem in programs with GUI.

The practical problem is that I've a mainform,
from which a number of popup forms are opened.
These forms are kept in a list.
When the mainform is closed,
it tries to close all the forms in the list.
Now the problem occurs when a subform closes itself,
when the mainform closes, Python crashes.
Yes I could send a message from the subform to the mainform,
but that makes my program unnecessary complex.

thanks,
Stef Mientki


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: is there a method to test if an (wx) object exists ?

Robin Dunn
Stef Mientki wrote:

> hi,
>
> Is there a method to test if an (wx) object exists ?
>
> This might a general Python question,
> but I couldn't find anything on the regular Python list,
> and I guess this might be a regular problem in programs with GUI.
>
> The practical problem is that I've a mainform,
> from which a number of popup forms are opened.
> These forms are kept in a list.
> When the mainform is closed,
> it tries to close all the forms in the list.
> Now the problem occurs when a subform closes itself,
> when the mainform closes, Python crashes.
> Yes I could send a message from the subform to the mainform,
> but that makes my program unnecessary complex.

When the C++ window objects are destroyed there is a check made if there
is any extra references to its Python proxy object.  If so then the
__class__ is changed to an object that raises an exception if you try to
access any of its attributes, and it also has a __nonzero__ magic
method.  So you can test if the UI object still exists with just an if
statement, something like this:

        for form in self.listOfForms:
                if form:
                        form.Close()

Another possibility to try is to use weak references when storing it in
the list, then if it is destroyed it will automatically be removed from
the list.

--
Robin Dunn
Software Craftsman
http://wxPython.org  Java give you jitters?  Relax with wxPython!



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: is there a method to test if an (wx) object exists ?

Stef Mientki-2

>
> When the C++ window objects are destroyed there is a check made if
> there is any extra references to its Python proxy object.  If so then
> the __class__ is changed to an object that raises an exception if you
> try to access any of its attributes, and it also has a __nonzero__
> magic method.  So you can test if the UI object still exists with just
> an if statement, something like this:
>
>     for form in self.listOfForms:
>         if form:
>             form.Close()
>
thanks Robin,
that works perfectly.
> Another possibility to try is to use weak references when storing it
> in the list, then if it is destroyed it will automatically be removed
> from the list.
>
ok, I try to remember that for later (have enough other things to learn ;-)

cheers,
Stef Mientki



Loading...