def Start(self, num_attempts): wx.CallAfter(self.progressbar.SetRange, num_attempts + 1) wx.CallAfter(self.progressbar.SetValue, 1) # range is 0..num_attempts inclusive. 0 shows no progress. wx.CallAfter(self.btnCancelClose.SetFocus) """Start Computation.""" # Trigger the worker thread unless it's already busy if not self.worker: wx.CallAfter(self.status.SetLabel, 'Starting Layout') self.worker = WorkerThread(self, self.blackboard, num_attempts)
class MainBlackboardFrame(FrameDeepLayout): def __init__(self, *args, **kwargs): super(MainBlackboardFrame, self).__init__(*args, **kwargs) self.InitUI() def InitUI(self): self.status = self.m_staticText3 self.progressbar = self.m_gauge1 self.Bind(wx.EVT_CLOSE, self.OnClose) # Set up event handler for any worker thread results EVT_RESULT(self, self.OnResult) self.Bind( wx.EVT_CHAR_HOOK, self.onKeyPress ) # http://stackoverflow.com/questions/3570254/in-wxpython-how-do-you-bind-a-evt-key-down-event-to-the-whole-window self.working = False # And indicate we don't have a worker thread yet self.worker = None self.blackboard = None def Start(self, num_attempts): wx.CallAfter(self.progressbar.SetRange, num_attempts + 1) wx.CallAfter( self.progressbar.SetValue, 1) # range is 0..num_attempts inclusive. 0 shows no progress. wx.CallAfter(self.btnCancelClose.SetFocus) """Start Computation.""" # Trigger the worker thread unless it's already busy if not self.worker: wx.CallAfter(self.status.SetLabel, 'Starting Layout') self.worker = WorkerThread(self, self.blackboard, num_attempts) def onKeyPress(self, event): keycode = event.GetKeyCode( ) # http://www.wxpython.org/docs/api/wx.KeyEvent-class.html if self.working: event.Skip() return self.working = True if keycode == wx.WXK_ESCAPE: print "ESC key detected in Blackboard: Abort Layout" self.OnClose(None) self.working = False event.Skip() def StopComputation(self): """Stop Computation.""" # Flag the worker thread to stop if running if self.worker: wx.CallAfter(self.status.SetLabel, 'Trying to abort') self.worker.abort() def OnClose(self, event): self.StopComputation() wx.FutureCall(500, self.Destroy) def OnCancelClick(self, event): if self.btnCancelClose.GetLabel() == 'Close': self.Destroy() self.StopComputation() wx.CallAfter(self.btnCancelClose.SetLabel, 'Close') def SetBlackboardObject(self, b): self.blackboard = b def OnResult(self, event): """Show Result status.""" #print event def log(msg): wx.CallAfter(self.m_textCtrl1.AppendText, msg + "\n") if event.logmsg: log(event.logmsg) if event.cmd: if event.cmd == 'snapshot_mgr_restore_0': wx.CallAfter(self.blackboard.umlwin.snapshot_mgr.Restore, 0) wx.CallAfter(self.btnCancelClose.SetFocus) elif event.cmd == 'stateofthenation': wx.CallAfter(self.blackboard.umlwin.stateofthenation) if event.statusmsg: wx.CallAfter(self.status.SetLabel, event.statusmsg) log("** " + event.statusmsg) if event.progress <> -1: wx.CallAfter(self.progressbar.SetValue, event.progress) if event.shouldStop: # the worker is done self.worker = None wx.CallAfter(self.btnCancelClose.SetLabel, 'Close')
class MainBlackboardFrame(FrameDeepLayout): def __init__(self, *args, **kwargs): super(MainBlackboardFrame, self).__init__(*args, **kwargs) self.InitUI() def InitUI(self): self.status = self.m_staticText3 self.progressbar = self.m_gauge1 self.Bind(wx.EVT_CLOSE, self.OnClose) # Set up event handler for any worker thread results EVT_RESULT(self, self.OnResult) self.Bind(wx.EVT_CHAR_HOOK, self.onKeyPress) # http://stackoverflow.com/questions/3570254/in-wxpython-how-do-you-bind-a-evt-key-down-event-to-the-whole-window self.working = False # And indicate we don't have a worker thread yet self.worker = None self.blackboard = None def Start(self, num_attempts): wx.CallAfter(self.progressbar.SetRange, num_attempts + 1) wx.CallAfter(self.progressbar.SetValue, 1) # range is 0..num_attempts inclusive. 0 shows no progress. wx.CallAfter(self.btnCancelClose.SetFocus) """Start Computation.""" # Trigger the worker thread unless it's already busy if not self.worker: wx.CallAfter(self.status.SetLabel, 'Starting Layout') self.worker = WorkerThread(self, self.blackboard, num_attempts) def onKeyPress(self, event): keycode = event.GetKeyCode() # http://www.wxpython.org/docs/api/wx.KeyEvent-class.html if self.working: event.Skip() return self.working = True if keycode == wx.WXK_ESCAPE: print "ESC key detected in Blackboard: Abort Layout" self.OnClose(None) self.working = False event.Skip() def StopComputation(self): """Stop Computation.""" # Flag the worker thread to stop if running if self.worker: wx.CallAfter(self.status.SetLabel, 'Trying to abort') self.worker.abort() def OnClose(self, event): self.StopComputation() wx.FutureCall(500, self.Destroy) def OnCancelClick(self, event): if self.btnCancelClose.GetLabel() == 'Close': self.Destroy() self.StopComputation() wx.CallAfter(self.btnCancelClose.SetLabel, 'Close') def SetBlackboardObject(self, b): self.blackboard = b def OnResult(self, event): """Show Result status.""" #print event def log(msg): wx.CallAfter(self.m_textCtrl1.AppendText, msg + "\n") if event.logmsg: log(event.logmsg) if event.cmd: if event.cmd == 'snapshot_mgr_restore_0': wx.CallAfter(self.blackboard.umlwin.snapshot_mgr.Restore, 0) wx.CallAfter(self.btnCancelClose.SetFocus) elif event.cmd == 'stateofthenation': wx.CallAfter(self.blackboard.umlwin.stateofthenation) if event.statusmsg: wx.CallAfter(self.status.SetLabel, event.statusmsg) log("** " + event.statusmsg) if event.progress <> -1: wx.CallAfter(self.progressbar.SetValue, event.progress) if event.shouldStop: # the worker is done self.worker = None wx.CallAfter(self.btnCancelClose.SetLabel, 'Close')