Skip to content Skip to sidebar Skip to footer

How To Pass Arguments To Win32com Event Handler

The code below works fine. I can't find a way to pass some arguments to EventHandler or to call methods of MainClass from EventHandler. For example instead of using constant param,

Solution 1:

One possibility is to use WithEvents function. But this may not be the best way. Also now handler and client objects are different entities, so this leads to additional mechanisms of interaction between them.

classEventHandler:

    defset_params(self, client):
        self.client = client

    defOnConnected(self):
        print"connected!"
        self.client.do_something()
        returnTrue

client = win32com.client.Dispatch("Lib.Obj")
handler = win32com.client.WithEvents(client, EventHandler)
handler.set_client(client)

client.connect()

whileTrue:
    PumpWaitingMessages()
    time.sleep(1)

Here is a complete example.

Post a Comment for "How To Pass Arguments To Win32com Event Handler"