event handling

Mark Hammond mhammond at skippinet.com.au
Fri Apr 4 01:12:03 CEST 2003


Miranda Evans wrote:
> In the following code, the while loop is written such that it
> terminates when either the double click event in the Excel object
> happens or ten seconds has elapsed...whichever comes first.
> 
> import time, win32event, win32com.client, pythoncom
> class xlEv:
>     def OnSheetBeforeDoubleClick(self, sh, target, cancel):
>        win32event.SetEvent(self.event)
> 
> def demo():
>     xl=win32com.client.DispatchWithEvents("Excel.Application", xlEv)
>     xl.event = win32event.CreateEvent(None, 0, 0, None) # work around
>     xl.Visible=1
>     xl.Workbooks.Add()
>     start_time = time.time()
>     while 1:
>        rc = win32event.MsgWaitForMultipleObjects((xl.event,),
>             0, 1000, win32event.QS_ALLEVENTS)
>        if rc == win32event.WAIT_OBJECT_0:
>           print 'event signalled'
>           break
>        elif rc == win32event.WAIT_OBJECT_0+1:
>           pythoncom.PumpWaitingMessages()
>        # check for timeout
>        if time.time() - start_time > 15:
>           print 'timed out'
>           break
> 
> demo()
> 
> Suppose I want to augment the demo() method so that it continues to do
> what it's already doing (waiting for either the double click event or
> for 10 seconds to elapse) plus one more thing: if the double click
> event is detected, display the values of the sh, target and cancel
> arguments.
> 
> How would one modify the demo() method to do that?

You would would create a new win32event object, and the double-click 
handler would set this event.  The main loops MsgWaitForMultipleObjects 
would get two events passed as the first param - this would make 
WAIT_OBJECT_0+1 your new event, and WAIT_OBJECT_0+2 now the "message 
available" event.

Mark.





More information about the Python-list mailing list