def subscribe(self, callback=None): if callback is not None and callable(callback): class Callback: def OnReceived(self): callback() client.WithEvents(self.__client__, Callback) self.__client__.Subscribe()
def __init__(self, name, events): thid = threading.current_thread().ident if thid in _storage: _GlobalDictionaries = _storage[thid] else: _GlobalDictionaries = client.gencache.EnsureDispatch("GSD.ELDictionaries") _storage[thid] = _GlobalDictionaries self.name = name self._GD = _GlobalDictionaries.GetDictionary(name) self._handler = client.WithEvents(self._GD, events) type(self)._instances.add(self) # Store this instance in the class set _instances
def executeTestModule(self, win32, test_module): print(test_module) seq = test_module.Sequence for i in range(1, seq.Count + 1): tc = win32.CastTo(seq.Item(i), "ITestCase") print(tc) # win32.WithEvents(test_module, TestModuleEvents) test_module.Start() global testComplete while not testComplete: pythoncom.PumpWaitingMessages() time.sleep(1) print(self.getVerdict(test_module.Verdict))
def __init__(self, guiPanel, changeEventFunc): global guiQtPanel global omnirigObject global changeEventFunction self.omniRigActive = False guiQtPanel = guiPanel changeEventFunction = changeEventFunc try: pythoncom.CoInitialize() omnirigObject = win32.gencache.EnsureDispatch('OmniRig.OmniRigX') self.omniRigActive = True except: guiQtPanel.setOmniRigErrorText('OmniRig connection error') guiQtPanel.disableControls() if self.omniRigActive: win32.WithEvents(omnirigObject, OmniRigEventsHandler)
def __init__(self): self.chart = client.Dispatch("CpForeDib.OvFutureChart") self.current_price = 0 self.running = False class EventHandler(object): def OnReceived(this): self.current_price = self.current.GetHeaderValue(7) price = Price.objects.create(value=self.current_price) print 'current', price.created, price.value self.exit_if_matched() self.current = client.Dispatch("CpForeDib.OvFutCur") client.WithEvents(self.current, EventHandler) self.current.SetInputValue(0, 'CLX14') self.current.Subscribe() print 'subscribed'
# Define our Application Events class ApplicationEvents: # define an event inside of the application, Be aware OnSheetActivate Syntax! def OnSheetActivate(self, *args): print("汇报~你已经选中了这个Sheet~") # Define our Workbook Events class WorkbookEvents: # define an event inside of the workbook, Be aware OnSheetSelectionChange Syntax! def OnSheetSelectionChange(self, *args): print(args[1].Address) # get the instance which is activated right now excel1 = win32.GetActiveObject("Excel.Application") # assign our event to the Excel Object excel1_events = win32.WithEvents(excel1, ApplicationEvents) # Get our workbook (Remember to enter the correct current workbook name(例如:工作簿2)) excel1_workbook = excel1.Workbooks("工作簿2") # assign our event to the workbook excel1_workbook_events = win32.WithEvents(excel1_workbook, WorkbookEvents) while True: # display the message pythoncom.PumpWaitingMessages()
def Subscribe(self): handler = w32c.WithEvents(self.obj, CpEvent) handler.set_params(self.obj, self.name, None)
class WorkbookEvents: # define an event inside of our Workbook def OnSheetSelectionChange(self, *args): #print the arguments print(args) print(args[1].Address) args[0].Range('A1').Value = 'You selected cell ' + str(args[1].Address) # Get the active instance of Excel xl = win32.GetActiveObject('Excel.Application') # assign our event to the Excel Application Object xl_events = win32.WithEvents(xl, ApplicationEvents) # grab the workbook xl_workbook = xl.Workbooks('PythonEventsFromExcel.xlsm') # assign events to Workbook xl_workbook_events = win32.WithEvents(xl_workbook, WorkbookEvents) # define initalizer keepOpen = True # while there are messages keep displaying them, and also as long as the Excel App is still open while keepOpen: # display the message pythoncom.PumpWaitingMessages()
import win32com.client as win32 #client是客户端的意思,常见的cs结构,即client/server. import pythoncom #给应用内定义事件,事件在此处由定义的函数实现,定义的函数是由windows定义的,因此函数名称不能错 class AapplicationEvents: def OnSheetActivate(self,*args): print("该工作表被选中了") class WorkBookEvents: def OnSheetSelectionChange(self,*args): print(args[1].Address) #获取激活的excel应用对象 excel1 = win32.GetActiveObject("Excel.Application") #监听选中sheet的事件,sheet被选中后,输出写好的print内容 excel1_events = win32.WithEvents(excel1,AapplicationEvents) #获取指定的excel,Workbooks方法是特有名词,不能拼写错误 excel2 = excel1.Workbooks("工作簿1") #监听工作簿1内选择cell的事件,输出选取的cell地址 excel2_events = win32.WithEvents(excel2,WorkBookEvents) while True: #使用pythoncom弹出消息 pythoncom.PumpWaitingMessages()