<s> import time <EOL> from threading import * <EOL> import wx <EOL> ID_START = wx . NewId ( ) <EOL> ID_STOP = wx . NewId ( ) <EOL> EVT_RESULT_ID = wx . NewId ( ) <EOL> def EVT_RESULT ( win , func ) : <EOL> """<STR_LIT>""" <EOL> win . Connect ( - <NUM_LIT:1> , - <NUM_LIT:1> , EVT_RESULT_ID , func ) <EOL> class ResultEvent ( wx . PyEvent ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , data ) : <EOL> """<STR_LIT>""" <EOL> wx . PyEvent . __init__ ( self ) <EOL> self . SetEventType ( EVT_RESULT_ID ) <EOL> self . data = data <EOL> class WorkerThread ( Thread ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , notify_window ) : <EOL> """<STR_LIT>""" <EOL> Thread . __init__ ( self ) <EOL> self . _notify_window = notify_window <EOL> self . _want_abort = <NUM_LIT:0> <EOL> self . start ( ) <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> for i in range ( <NUM_LIT:10> ) : <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> if self . _want_abort : <EOL> wx . PostEvent ( self . _notify_window , ResultEvent ( None ) ) <EOL> return <EOL> wx . PostEvent ( self . _notify_window , ResultEvent ( <NUM_LIT:10> ) ) <EOL> def abort ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _want_abort = <NUM_LIT:1> <EOL> class MainFrame ( wx . Frame ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , parent , id ) : <EOL> """<STR_LIT>""" <EOL> wx . Frame . __init__ ( self , parent , id , '<STR_LIT>' ) <EOL> wx . Button ( self , ID_START , '<STR_LIT>' , pos = ( <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> wx . Button ( self , ID_STOP , '<STR_LIT>' , pos = ( <NUM_LIT:0> , <NUM_LIT:50> ) ) <EOL> self . status = wx . StaticText ( self , - <NUM_LIT:1> , '<STR_LIT>' , pos = ( <NUM_LIT:0> , <NUM_LIT:100> ) ) <EOL> self . Bind ( wx . EVT_BUTTON , self . OnStart , id = ID_START ) <EOL> self . Bind ( wx . EVT_BUTTON , self . OnStop , id = ID_STOP ) <EOL> EVT_RESULT ( self , self . OnResult ) <EOL> self . worker = None <EOL> def OnStart ( self , event ) : <EOL> """<STR_LIT>""" <EOL> if not self . worker : <EOL> self . status . SetLabel ( '<STR_LIT>' ) <EOL> self . worker = WorkerThread ( self ) <EOL> def OnStop ( self , event ) : <EOL> """<STR_LIT>""" <EOL> if self . worker : <EOL> self . status . SetLabel ( '<STR_LIT>' ) <EOL> self . worker . abort ( ) <EOL> def OnResult ( self , event ) : <EOL> """<STR_LIT>""" <EOL> if event . data is None : <EOL> self . status . SetLabel ( '<STR_LIT>' ) <EOL> else : <EOL> self . status . SetLabel ( '<STR_LIT>' % event . data ) <EOL> self . worker = None <EOL> class MainApp ( wx . App ) : <EOL> """<STR_LIT>""" <EOL> def OnInit ( self ) : <EOL> """<STR_LIT>""" <EOL> self . frame = MainFrame ( None , - <NUM_LIT:1> ) <EOL> self . frame . Show ( True ) <EOL> self . SetTopWindow ( self . frame ) <EOL> return True <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> app = MainApp ( <NUM_LIT:0> ) <EOL> app . MainLoop ( ) </s>
