[Python-de] IExp Fernsteuerung

Oliver Schaefer SchaeferFFM at gmx.de
Wed Feb 26 20:29:57 EST 2003


Florian Fredegar Haftmann wrote:
[schnipp]
> Was hier passieren soll: Internet Explorer wird "ferngesteuert", sobald 
> das Dokument da ist, wird hiner alle Buttons ein Eventhandler für Klicks 
> gehängt.
> 
> Problem: Wie "verpack" ich die Eventhandler!?

In $PREFIX/Lib/site-packages/win32com/client/__init__.py
gibt es u.a. die Funktion
     def DispatchWithEvents(clsid, user_event_class)
mit einem Beispiel, um Events abzufangen.

Es wird eine Klasse benötigt wie die folgende:
(siehe 
$PREFIX\Lib\site-packages\win32com\gen_py\EAB22AC0-30C1-11CF-A7EB-0000C05BAE0Bx0x1x1.py
für Details)

class IWebBrowserEvents2:
     "Web Browser Control events interface"
	# Handlers for the control
	# If you create handlers, they should have the following prototypes:
# 
def OnWindowSetTop(self, Top=defaultNamedNotOptArg):
# 
	"""Fired when the host window should change its Top coordinate"""
# 
def OnStatusTextChange(self, Text=defaultNamedNotOptArg):
# 
	"""Statusbar text changed."""
# 
def OnWindowClosing(self, IsChildWindow=defaultNamedNotOptArg, 
Cancel=defaultNamedNotOptArg):
# 
	"""Fired when the WebBrowser is about to be closed by script"""
# 
def OnFileDownload(self, Cancel=defaultNamedNotOptArg):
# 
	"""Fired to indicate the File Download dialog is opening"""
# 
def OnStatusBar(self, StatusBar=defaultNamedNotOptArg):
# 
	"""Fired when the statusbar should be shown/hidden"""
# 
def OnWindowSetResizable(self, Resizable=defaultNamedNotOptArg):
# 
	"""Fired when the host window should allow/disallow resizing"""
# 
def OnNewWindow2(self, ppDisp=defaultNamedNotOptArg, 
Cancel=defaultNamedNotOptArg):
# 
	"""A new, hidden, non-navigated WebBrowser window is needed."""
# 
def OnDownloadComplete(self):
# 
	"""Download of page complete."""
# 
def OnBeforeNavigate2(self, pDisp=defaultNamedNotOptArg, 
URL=defaultNamedNotOptArg, Flags=defaultNamedNotOptArg, 
TargetFrameName=defaultNamedNotOptArg, PostData=defaultNamedNotOptArg, 
Headers=defaultNamedNotOptArg, Cancel=defaultNamedNotOptArg):
# 
	"""Fired before navigate occurs in the given WebBrowser (window or 
frameset element). The processing of this navigation may be modified."""
# 
def OnSetSecureLockIcon(self, SecureLockIcon=defaultNamedNotOptArg):
# 
	"""Fired to indicate the security level of the current web page contents"""
# 
def OnProgressChange(self, Progress=defaultNamedNotOptArg, 
ProgressMax=defaultNamedNotOptArg):
# 
	"""Fired when download progress is updated."""
# 
def OnCommandStateChange(self, Command=defaultNamedNotOptArg, 
Enable=defaultNamedNotOptArg):
# 
	"""The enabled state of a command changed."""
# 
def OnClientToHostWindow(self, CX=defaultNamedNotOptArg, 
CY=defaultNamedNotOptArg):
# 
	"""Fired to request client sizes be converted to host window sizes"""
# 
def OnDownloadBegin(self):
# 
	"""Download of a page started."""
# 
def OnWindowSetWidth(self, Width=defaultNamedNotOptArg):
# 
	"""Fired when the host window should change its width"""
# 
def OnFullScreen(self, FullScreen=defaultNamedNotOptArg):
# 
	"""Fired when fullscreen mode should be on/off"""
# 
def OnDocumentComplete(self, pDisp=defaultNamedNotOptArg, 
URL=defaultNamedNotOptArg):
# 
	"""Fired when the document being navigated to reaches ReadyState_Complete."""
# 
def OnMenuBar(self, MenuBar=defaultNamedNotOptArg):
# 
	"""Fired when the menubar should be shown/hidden"""
# 
def OnQuit(self):
# 
	"""Fired when application is quiting."""
# 
def OnWindowSetLeft(self, Left=defaultNamedNotOptArg):
# 
	"""Fired when the host window should change its Left coordinate"""
# 
def OnPropertyChange(self, szProperty=defaultNamedNotOptArg):
# 
	"""Fired when the PutProperty method has been called."""
# 
def OnTitleChange(self, Text=defaultNamedNotOptArg):
# 
	"""Document title changed."""
# 
def OnWindowSetHeight(self, Height=defaultNamedNotOptArg):
# 
	"""Fired when the host window should change its height"""
# 
def OnNavigateComplete2(self, pDisp=defaultNamedNotOptArg, 
URL=defaultNamedNotOptArg):
# 
	"""Fired when the document being navigated to becomes visible and enters 
the navigation stack."""
# 
def OnToolBar(self, ToolBar=defaultNamedNotOptArg):
# 
	"""Fired when the toolbar  should be shown/hidden"""
# 
def OnTheaterMode(self, TheaterMode=defaultNamedNotOptArg):
# 
	"""Fired when theater mode should be on/off"""
# 
def OnVisible(self, Visible=defaultNamedNotOptArg):
# 
	"""Fired when the window should be shown/hidden"""


Beispiel (ungetestet - Ausschnitt aus einem großen package):

import sys
from win32com.client import constants, DispatchWithEvents
from pythoncom import com_error, Missing
from time import sleep

optArg = defaultNamedNotOptArg = Missing
ready  = 4   # constants.READYSTATE_COMPLETE


class InternetExplorerEvents:
     "Web Browser Control events interface suitable for debugging."
     _write = sys.stderr.write
     def set_debug(self, write=sys.stderr.write):
         self._write = write

     def OnVisible(self, Visible=optArg):
         self._write('OnVisible(%s)\n' % `Visible`)
     def OnTitleChange(self, Text=optArg):
         self._write('OnTitleChange(%s)\n' % `Text`)
     def OnDownloadBegin(self):
         self._write('OnDownloadBegin()\n')
     def OnQuit(self):
         self._write('OnQuit()\n')
     def OnCommandStateChange(self, Command=optArg, Enable=optArg):
         self._write('OnCommandStateChange(%s, %s)\n' % (`Command`,
                                                         `Enable`))
     def OnToolBar(self, ToolBar=optArg):
         self._write('OnToolBar(%s)\n' % `ToolBar`)
     def OnNavigateComplete2(self, pDisp=optArg, URL=optArg):
         self._write('OnNavigateComplete2(%s, %s)\n' % (`pDisp`, `URL`))
     def OnBeforeNavigate2(self, pDisp=optArg, URL=optArg, Flags=optArg,
                           TargetFrameName=optArg, PostData=optArg,
                           Headers=optArg, Cancel=optArg):
         self._write('OnBeforeNavigate2('
                     '%s, %s, %s, %s, '
                     '%s, %s, %s)\n' % (`pDisp`, `URL`, `Flags`,
                                        `TargetFrameName`, `PostData`,
                                        `Headers`, `Cancel`))
     def OnNewWindow2(self, ppDisp=optArg, Cancel=optArg):
         self._write('OnNewWindow2(%s, %s)\n' % (`ppDisp`, `Cancel`))
     def OnTheaterMode(self, TheaterMode=optArg):
         self._write('OnTheaterMode(%s)\n' % `TheaterMode`)
     def OnProgressChange(self, Progress=optArg, ProgressMax=optArg):
         self._write('OnProgressChange(%s, %s)\n' % (`Progress`,
                                                     `ProgressMax`))
     def OnStatusBar(self, StatusBar=optArg):
         self._write('OnStatusBar(%s)\n' % `StatusBar`)
     def OnPropertyChange(self, szProperty=optArg):
         self._write('OnPropertyChange(%s)\n' % `szProperty`)
     def OnMenuBar(self, MenuBar=optArg):
         self._write('OnMenuBar(%s)\n' % `MenuBar`)
     def OnStatusTextChange(self, Text=optArg):
         self._write('OnStatusTextChange(%s)\n' % `Text`)
     def OnDocumentComplete(self, pDisp=optArg, URL=optArg):
         self._write('OnDocumentComplete(%s, %s)\n' % (`pDisp`, `URL`))
     def OnDownloadComplete(self):
         self._write('OnDownloadComplete()\n')
     def OnFullScreen(self, FullScreen=optArg):
         self._write('OnFullScreen(%s)\n' % `FullScreen`)


def browse(url=None, write=sys.stderr.write):
     """Test drive IE.

     Usage:
         >>> import WebBrowserEvents
         >>> url = None   # browse docs...
         >>> WebBrowserEvents.browse(url)
     """
     ie = DispatchWithEvents('InternetExplorer.Application',
                             InternetExplorerEvents)
     ie.set_debug(write=write)

     if url is None:
         url = r'file:/%s\Doc\index.html' % sys.prefix

     ie.Navigate(url)

     try:
         while ie.ReadyState != ready:
             sleep(0.1)

         document = ie.Document
         all      = document.all
         window   = document.frames
         screen   = window.screen

         if not window.opener and window.parent == window.self:
             window.moveTo(0, 0)
             window.resizeTo(screen.availWidth, screen.availHeight)
     except com_error, err_msg:
         write('--> %s: %s\n' % (com_error.__name__, err_msg))

         ie.Left   = 0
         ie.Top    = 0
         ie.Width  = 1024
         ie.Height = 741

     ie.Visible = 1

     return ie


def test(url, wait=1, write=sys.stderr.write):
     """Test drive IE.

     Usage:
         >>> import WebBrowserEvents, sys
         >>> url = r'file:/%s\Doc\index.html' % sys.prefix
         >>> WebBrowserEvents.test(url)
     """
     ie = DispatchWithEvents('InternetExplorer.Application',
                             InternetExplorerEvents)
     ie.set_debug(write=write)

     if 0:
         ie.Left   = 0
         ie.Top    = 0
         ie.Width  = 1024
         ie.Height = 741

     ie.Visible = 1

     # write('--> About to Navigate...\n')
     ie.Navigate(url)

     try:
         while ie.ReadyState != ready:
             sleep(0.01)

         document = ie.Document
         all      = document.all
         window   = document.frames
         screen   = window.screen

         if not window.opener and window.parent == window.self:
             window.moveTo(0, 0)
             window.resizeTo(screen.availWidth, screen.availHeight)
     except com_error, err_msg:
         write('--> %s: %s\n' % (com_error.__name__, err_msg))

     if not wait:
         while ie.Busy:
             sleep(0.01)

         write('--> Use Quit() to exit from IE\n')
         return ie

     while ie.Visible:
         sleep(0.01)
     # write('--> IE not visible any more\n')

     if 0:
         try:
             write('--> Trying to quit\n')
             ie.Quit()
             write('--> Quit succeeded\n')
         except com_error, err_msg:
             write('--> %s: %s\n' % (com_error.__name__, err_msg))
     # write('--> Exit\n')


if __name__ == '__main__':
     if len(sys.argv) == 1:
         url = raw_input('url: ')
         if not url:
             # url = 'http://www.python.org/index.html'
             url = r'file:/%s\Python\Doc\index.html' % sys.prefix
     else:
         url = sys.argv[1]

     test(url)





More information about the Python-de mailing list