constructor inquiry
Miranda Evans
mirandacascade at yahoo.com
Fri Apr 4 12:17:00 EST 2003
When the demo() method below is run and one double clicks in a cell in
the Excel spreadsheet, the messages that get displayed are:
in OnSheetBeforeDoubleClick event
xl.event signalled
dbl click
It appears as though the constructor for the xlEv class never got
invoked, but the OnSheetBeforeDoubleClick method of the xlEv class got
invoked. In addition, it appears as though the attribute 'type' gets
set in the OnSheetBeforeDoubleClick method and that the demo() method
has visibility to this attribute. The fact that the print xl.type
statement results in the text
dbl click
being printed on the screen is why I believe the demo() method has
visibility to this attribute.
My questions are:
1) is it correct that the constructor for the xlEv class never got
invoked? if so, is it correct to infer that in the sample code below,
at no time does an object of class xlEv get instantiated?
2) is it correct to infer that a class method (e.g.
OnSheetBeforeDoubleClick) can get invoked without an instance of the
class to which that method belongs getting instantiated?
3) is the 'xl' object in the demo() method an object of class xlEv?
if so, how could the xl object come into existence without the
constructor getting invoked? if not, how could the xl object have
visibility to the type attribute?
import time
import win32event
import win32com.client
import pythoncom
class xlEv:
def __init__(self):
print 'in constructor'
self.type = ""
def OnSheetBeforeDoubleClick (self, sh, target, cancel):
print 'in OnSheetBeforeDoubleClick event'
self.type = "dbl click"
win32event.SetEvent(self.event)
def demo():
# create an instance of an Excel app; tie it to the xlEv class
xl = win32com.client.DispatchWithEvents("Excel.Application", xlEv)
xl.event = win32event.CreateEvent(None, 0, 0, None)
# for the purpose of this demo, make the worksheet visible and
# clickable; assumes user may double click in a cell
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:
# Event signalled.
print 'xl.event signalled'
print xl.type
break
elif rc == win32event.WAIT_OBJECT_0+1:
pythoncom.PumpWaitingMessages()
if time.time()-start_time > 10:
print 'time out'
break
More information about the Python-list
mailing list