event handling

Miranda Evans mirandacascade at yahoo.com
Wed Apr 2 18:29:13 EST 2003


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?




More information about the Python-list mailing list