How to close new thread created by on_trait_change when mian thread is terminated

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

How to close new thread created by on_trait_change when mian thread is terminated

Ariman
Hi, I write an application with TraitsGUI and Chaco, one line of its codes is: self.on_trait_change(self.get_running_memo, 'plot', dispatch = 'new') this runs my method 'get_running_memo' in a new thread, which is I need. there is a loop in method 'get_running_memo', so before closing application, I must stop this loop. I can break this loop using Action, I mean I create a Close button to controll the loop. But my problem is how to deal with the windows close button [x], the red one on the top right corner. If click [x] when process is looping, I will get the following errors and the thread seems to be never ended, the error is: Exception occurred in traits notification handler for object: <__main__.MemoPlot object at 0x031E0870>, trait: value, old value: [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 444 444 444 444 444 444 444 444 444 444 444], new value: [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 444 444 444 444 444 444 444 444 444 444 444 444] Traceback (most recent call last): File "C:\Python26\lib\site-packages\traits-3.2.0-py2.6-win32.egg\enthought\traits\trait_notifiers.py", line 475, in rebind_call_0 self.dispatch( getattr( self.object(), self.name ) ) File "C:\Python26\lib\site-packages\traits-3.2.0-py2.6-win32.egg\enthought\traits\trait_notifiers.py", line 594, in dispatch ui_handler( handler, *args ) File "C:\Python26\lib\site-packages\traitsbackendwx-3.2.0-py2.6.egg\enthought\traits\ui\wx\toolkit.py", line 95, in ui_handler wx.CallAfter( handler, *args ) File "C:\Python26\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py", line 14613, in CallAfter assert app is not None, 'No wx.App created yet' AssertionError: No wx.App created yet can anyone give me some helps about this, how to disable the [x], or how to make the new thread created by 'on_tarit_change' terminated when main thread is closed. Thanks very much.
Reply | Threaded
Open this post in threaded view
|

Re: How to close new thread created by on_trait_change when mian thread is terminated

Ariman
Hi, I write an application with TraitsGUI and Chaco, one line of its codes is:
   self.on_trait_change(self.get_running_memo, 'plot', dispatch = 'new')
This runs my method 'get_running_memo' in a new thread, which is I need. there is a loop in method 'get_running_memo', so before closing application, I must stop this loop. I can break this loop using Action, I mean I create a Close button to controll the loop. But my problem is how to deal with the windows close button [x], the red one on the top right corner. If click [x] when process is looping, I will get the following errors and the thread seems to be never ended, the error is:

    Exception occurred in traits notification handler for object: .... in CallAfter assert app is not None, 'No wx.App created yet' AssertionError: No wx.App created yet


Can anyone give me some helps about this, how to disable the [x], or how to make the new thread created by 'on_tarit_change' terminated when main thread is closed.

Thanks very much.
Reply | Threaded
Open this post in threaded view
|

Re: How to close new thread created by on_trait_change when mian thread is terminated

bryce hendrix-2


On Tue, Jan 25, 2011 at 9:01 PM, Ariman <[hidden email]> wrote:

Hi, I write an application with TraitsGUI and Chaco, one line of its codes
is:
  self.on_trait_change(self.get_running_memo, 'plot', dispatch = 'new')
This runs my method 'get_running_memo' in a new thread, which is I need.
there is a loop in method 'get_running_memo', so before closing application,
I must stop this loop. I can break this loop using Action, I mean I create a
Close button to controll the loop. But my problem is how to deal with the
windows close button [x], the red one on the top right corner. If click [x]
when process is looping, I will get the following errors and the thread
seems to be never ended, the error is:

   Exception occurred in traits notification handler for object: .... in
CallAfter assert app is not None, 'No wx.App created yet' AssertionError: No
wx.App created yet


Can anyone give me some helps about this, how to disable the [x], or how to
make the new thread created by 'on_tarit_change' terminated when main thread
is closed.

You'll have to create a subclass of Handler. You have two options:

 1. overload the init method and disable the close widget via wx (or Qt).
 2. overload the close method such that it returns False when you don't want to close the window.

I recommend option #2 unless you really, really want to grey out the close widget.

Bryce

_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: How to close new thread created by on_trait_change when mian thread is terminated

Ariman
Hi, Bryce, thanks a lot for your help, I've overloaded the close method, it works just as I wish.
Reply | Threaded
Open this post in threaded view
|

Re: How to close new thread created by on_trait_change when mian thread is terminated

Thomas Königstein
If it's not too much work, can you post the relevant code snippet please? 

Thanks, cheers

Thomas

On Wed, Jan 26, 2011 at 06:20, Ariman <[hidden email]> wrote:

Hi, Bryce, thanks a lot for your help, I've overloaded the close method, it
works just as I wish.
--
View this message in context: http://enthought-dev.117412.n3.nabble.com/How-to-close-new-thread-created-by-on-trait-change-when-mian-thread-is-terminated-tp2326869p2351850.html
Sent from the Enthought Dev mailing list archive at Nabble.com.
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: How to close new thread created by on_trait_change when mian thread is terminated

Ariman
Hi,

the relevant code of my application is too confused to be posted here. I can give you another simple example to show how to disable the close function, wish it can help you.

from enthought.traits.api import HasTraits, Str
from enthought.traits.ui.api import Handler, Item, View, EnumEditor
from enthought.traits.ui.menu import OKButton

class MyHandler(Handler):
   
    def close(self, info, is_ok):
        if info.object.name == 'enable close':
            return True
        if info.object.name == 'disable close':
            return False
        else:
            return True
       
class Main(HasTraits):
   
    name = Str
    list = ['enable close', 'disable close']
   
    trait_view = View(
                        Item('name', style='simple',
                             editor=EnumEditor(name='list')),
                        handler = MyHandler,
                        resizable = True,
                        buttons = [OKButton]
                      )
   
       
Main().configure_traits()
Reply | Threaded
Open this post in threaded view
|

Re: How to close new thread created by on_trait_change when mian thread is terminated

Thomas Königstein
Very helpful indeed, thank you!
One question, though, how did you know how the close() method is called, did you (like me, after seeing this example) search for the "handler.py" file in the enthought library and look for the close() definition (via ctrf-f), or is there a simpler way? 
Also, I didn't know yet that I can access the traits in Main with "info.object.name_of_trait" in the close method... and seemingly, object isn't even listed in the info attributes ( >>>"object" in dir(info)     >>> False ), still info.object does exists (apparently it's a pointer to/ a copy of the Main class instance).

So, this snippet is very useful to me I just wonder how I could have figured all of that out on my own.

Thanks, cheers

Thomas

On Wed, Jan 26, 2011 at 08:21, Ariman <[hidden email]> wrote:

Hi,

the relevant code of my application is too confused to be posted here. I can
give you another simple example to show how to disable the close function,
wish it can help you.

from enthought.traits.api import HasTraits, Str
from enthought.traits.ui.api import Handler, Item, View, EnumEditor
from enthought.traits.ui.menu import OKButton

class MyHandler(Handler):

   def close(self, info, is_ok):
       if info.object.name == 'enable close':
           return True
       if info.object.name == 'disable close':
           return False
       else:
           return True

class Main(HasTraits):

   name = Str
   list = ['enable close', 'disable close']

   trait_view = View(
                       Item('name', style='simple',
                            editor=EnumEditor(name='list')),
                       handler = MyHandler,
                       resizable = True,
                       buttons = [OKButton]
                     )


Main().configure_traits()
--
View this message in context: http://enthought-dev.117412.n3.nabble.com/How-to-close-new-thread-created-by-on-trait-change-when-mian-thread-is-terminated-tp2326869p2352183.html
Sent from the Enthought Dev mailing list archive at Nabble.com.
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: How to close new thread created by on_trait_change when mian thread is terminated

Robert Kern
2011/1/26 Thomas Königstein <[hidden email]>:
> Very helpful indeed, thank you!
> One question, though, how did you know how the close() method is called, did
> you (like me, after seeing this example) search for the "handler.py" file in
> the enthought library and look for the close() definition (via ctrf-f), or
> is there a simpler way?

http://code.enthought.com/projects/traits/docs/html/TUIUG/handler.html#overriding-standard-methods

--
Robert Kern
Enthought
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: How to close new thread created by on_trait_change when mian thread is terminated

Robert Kern
In reply to this post by Thomas Königstein
2011/1/26 Thomas Königstein <[hidden email]>:
> Very helpful indeed, thank you!
> One question, though, how did you know how the close() method is called, did
> you (like me, after seeing this example) search for the "handler.py" file in
> the enthought library and look for the close() definition (via ctrf-f), or
> is there a simpler way?

http://code.enthought.com/projects/traits/docs/html/TUIUG/handler.html#overriding-standard-methods

--
Robert Kern
Enthought
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: How to close new thread created by on_trait_change when mian thread is terminated

Ariman
In reply to this post by Thomas Königstein
Robert Kern's link is very helpful to you I think, though where I found the solution is:

http://code.enthought.com/projects/files/ETS32_API/enthought.traits.ui.handler.Handler.html