Python calls COM and COM events

xiaoxiao2021-03-06  56

Python calls COM and COM events

Author: Mei Jinsong

Email: stephen.cn@gmail.com

Time: September 13, 2004

Thank: Liu Xin

Python calls if there is a COM that occurs, you need some techniques.

We explain my experience with IE this COM.

First we need the support of the Pywin32 module, which provides us to call COM convenience. You can search for www.sf.net and download it.

Run as follows:

Import Win32gui

Import Win32com

Import win32com.client

Import pythoncom

Import Time

Class EventHandler:

Def onvisible (Self, Visible):

Global BvisibleEventFired

BVisibleEventFired = 1

DEF ONDOWNLOADBEGIN (SELF):

Print "Downloadbegin"

DEF ONDOWNLOADCOMPLETE (Self):

Print "DownloadComplete"

Def onDocumentComplete (self, pdisp = pythoncom.missing, url = pythoncom.missing):

Print "DocumentComplete Of% S"% URL

# Here with the EventHandler class to handle events that occur in IE, the function name here must match the event name.

IE = Win32com.client.dispatchwithevents ("InternetExplorer.Application", EventHandler)

IE.visible = 1

IE.NAVIGATE ("www.aawns.com")

# 这里 这里 等 等 事 事

Pythoncom.pumpmessages ()

IE.quit ()

We see that the program runs normally, opens our designated site, and each incident can be triggered and can make the correct reflection.

But if we want to call some of the methods and properties we have inherited after the incident. You will find that you cannot use.

The following code will show this example

# - * - CODING: CP936 - * -

Import Win32gui

Import Win32com

Import win32com.client

Import pythoncom

Import Time

Class test:

DEF RUNTEST (Self):

Print 'Tuntest'

Class EventHandler:

Def onvisible (Self, Visible):

Global BvisibleEventFired

BVisibleEventFired = 1

DEF ONDOWNLOADBEGIN (SELF):

Print "Downloadbegin"

DEF ONDOWNLOADCOMPLETE (Self):

Print "DownloadComplete"

Def onDocumentComplete (self, pdisp = pythoncom.missing, url = pythoncom.missing):

Print "DocumentComplete Of% S"% URL

# Here we call the TEST RUNTEST method to see if the inherit is successful.

Self.Runtest ()

Class Runcom (TEST):

DEF __INIT __ (Self):

IE = Win32com.client.dispatchwithevents ("InternetExplorer.Application", EventHandler) IE.Visible = 1

IE.NAVIGATE ("www.aawns.com")

# That is called Test's runtest method to see if it is successful.

Self.Runtest ()

Pythoncom.pumpmessages ()

IE.quit ()

a = runcom ()

The result is wrong.

TUNTEST

Downloadbegin

Downloadcomplete

Downloadbegin

Downloadcomplete

DocumentComplete of http://www.aawns.com/

Pythoncom error: Python Error Invoking COM METHOD.

TRACEBACK (MOST RECENT CALL LAST):

File "c: /python23/lib/site-packages/win32com/server/policy.py", line 283, in _

INVOKE_

Return Self._invoke_ (Dispid, LCID, WFLAGS, ARGS)

File "c: /python23/lib/site-packages/win32com/server/policy.py", line 288, in _

INVOKE_

Return S_OK, -1, Self._invokeex_ (Dispid, LCID, WFLAGS, ARGS, NONE, NONE)

File "c: /python23/lib/site-packages/win32com/server/policy.py", LINE 550, IN _

Invokeex_

Return Func (* args)

File "D: /Python/test.py", Line 24, in OONCUMETCOMPLETE

Self.Runtest ()

File "c: / python23 / lib / site-packages / win32com / client / __ init__.py", LINE 454, IN

__Getattr__

Raise AttributeError, "'% s' Object Has NO Attribute '% S'"% (Repr (Self), ATT

r)

Exceptions.attributeError: '

'Object Has No Attribute' Runtest '

We see the TEST class RunTest method is not inherited, why? My understanding is that the event mode of COM can't inherit the self in Python, because when the IE is called, it is not an instance of EventHandler to use the class as an event process (I don't know if it is correct, if it is correct, if Have better understanding. We exchange)

After finding a lot of information and a lot of methods, it is only possible to pass data between events and various classes. The code is changed to

# - * - CODING: CP936 - * -

Import Win32gui

Import Win32com

Import win32com.client

Import pythoncom

Import Time

Class EventHandler:

Def onvisible (Self, Visible):

Global BvisibleEventFired

BVisibleEventFired = 1

Def Ondownloadbegin (Self): Print "Downloadbegin"

# First inherit the global variable to add a string

Global Testlist

TestList.Append ("Downloadbegin")

DEF ONDOWNLOADCOMPLETE (Self):

Print "DownloadComplete"

# First inherit the global variable to add a string

Global Testlist

TestList.Append ("Downloadcomplete")

Def onDocumentComplete (self, pdisp = pythoncom.missing, url = pythoncom.missing):

Print "DocumentComplete Of% S"% URL

# First inherit the global variable and then print

Global Testlist

Print Testlist

Class Runcom:

DEF __INIT __ (Self):

Global Testlist

IE = Win32com.client.dispatchwithevents ("InternetExplorer.Application", EventHandler)

IE.visible = 1

IE.NAVIGATE ("www.aawns.com")

# Print global variables

Print Testlist

Pythoncom.pumpmessages ()

IE.quit ()

Testlist = []

a = runcom ()

It can be seen that the issue of transmitting data in the event is solved in the way.

No solution: There is also a corresponding event when using Twisted, how to ensure that both the Events in Twisted and COM are triggered?

转载请注明原文地址:https://www.9cbs.com/read-117367.html

New Post(0)