PyGtk 使用 IE 显示 HTML 的例子

Jiahua Huang <[email protected]>
sender-time     Sent at 21:58 (GMT+08:00). Current time there: 10:05 PM. ✆
reply-to        [email protected]
to      python-cn <[email protected]>
date    Fri, Oct 16, 2009 at 21:58

pygtkie.py

是 Pygtk-Notebook-Latest 里的一个浏览器例子,

小节标题1

   1 """
   2 Embedding IE in pygtk via AtlAxWin and ctypes.
   3 """
   4 # needs the comtypes package from http://sourceforge.net/projects/comtypes/
   5 import sys
   6 import pygtk
   7 pygtk.require("2.0")
   8 import gtk
   9 if sys.platform=="win32":
  10     import win32con
  11     from ctypes import *
  12     import ctypes.wintypes
  13     from comtypes import *
  14     from comtypes.automation import IUnknown 
  15     from comtypes.client import IDispatch, VARIANT 
  16     import wrap
  17     kernel32 = windll.kernel32
  18     user32 = windll.user32
  19     atl = windll.atl
  20 else:
  21     import gtkmozembed
  22 class GUI:
  23     def __init__(self):
  24         self.home_url = "http://www.majorsilence.com/"
  25         self.win = gtk.Window(gtk.WINDOW_TOPLEVEL)
  26         self.win.set_title("Example Web browser that works on Linux and Windows")
  27         self.win.connect("destroy", gtk.main_quit) 
  28         self.win.set_size_request(750, 550)
  29         self.win.realize()
  30         self.main_vbox = gtk.VBox()
  31         control_box = gtk.HBox(False, 0)
  32         back = gtk.Button("Back")
  33         forward = gtk.Button("Forward")
  34         refresh = gtk.Button("Refresh")
  35         stop = gtk.Button("Stop")
  36         home = gtk.Button("Home")
  37         self.address = gtk.Entry(max=0) # no limit on address length
  38         go = gtk.Button("Go")
  39         control_box.pack_start(back, True, True, 2)
  40         control_box.pack_start(forward, True, True, 2)
  41         control_box.pack_start(refresh, True, True, 2)
  42         control_box.pack_start(stop, True, True, 2)
  43         control_box.pack_start(home, True, True, 2)
  44         control_box.pack_start(self.address, True, True, 2)
  45         control_box.pack_start(go, True, True, 2)
  46         back.connect("clicked", self.on_backward_clicked, None)
  47         forward.connect("clicked", self.on_forward_clicked, None)
  48         refresh.connect("clicked", self.on_refresh_clicked, None)
  49         stop.connect("clicked", self.on_stop_clicked, None)
  50         home.connect("clicked", self.on_home_clicked, None)
  51         self.address.connect("key_press_event", self.on_address_keypress)
  52         go.connect("clicked", self.on_go_clicked, None)
  53         self.main_vbox.pack_start(control_box, False, True, 2)
  54         if sys.platform=="win32":
  55             self.init_ie()
  56         else:
  57             self.init_mozilla()
  58         self.win.add(self.main_vbox)
  59         self.win.show_all()
  60     def init_ie(self):
  61         # Create a DrawingArea to host IE and add it to the hbox.
  62         self.container = gtk.DrawingArea()
  63         self.main_vbox.add(self.container)
  64         self.container.show()
  65         # Make the container accept the focus and pass it to the control;
  66         # this makes the Tab key pass focus to IE correctly.
  67         self.container.set_property("can-focus", True)
  68         self.container.connect("focus", self.on_container_focus)
  69         # Resize the AtlAxWin window with its container.
  70         self.container.connect("size-allocate", self.on_container_size)
  71         # Create an instance of IE via AtlAxWin.
  72         atl.AtlAxWinInit()
  73         hInstance = kernel32.GetModuleHandleA(None)
  74         parentHwnd = self.container.window.handle
  75         self.atlAxWinHwnd = user32.CreateWindowExA(0, "AtlAxWin", self.home_url,
  76             win32con.WS_VISIBLE | win32con.WS_CHILD | win32con.WS_HSCROLL |
  77             win32con.WS_VSCROLL, 0, 0, 100, 100, parentHwnd, None, hInstance, 0)
  78         # Get the IWebBrowser2 interface for the IE control.
  79         pBrowserUnk = POINTER(IUnknown)()
  80         atl.AtlAxGetControl(self.atlAxWinHwnd, byref(pBrowserUnk))
  81         # the wrap call queries for the default interface
  82         self.browser = wrap(pBrowserUnk)
  83         # Create a Gtk window that refers to the native AtlAxWin window.
  84         self.gtkAtlAxWin = gtk.gdk.window_foreign_new(long(self.atlAxWinHwnd))
  85         # By default, clicking a GTK widget doesn't grab the focus away from
  86         # a native Win32 control.
  87         self.address.connect("button-press-event", self.on_widget_click)
  88     def init_mozilla(self):
  89         self.browser = gtkmozembed.MozEmbed()
  90         self.main_vbox.add(self.browser)
  91         self.browser.load_url(self.home_url)
  92     def on_backward_clicked(self, widget=None, data=None):
  93         if sys.platform=="win32":
  94             try:
  95                  self.browser.GoBack()
  96             except:
  97                  pass # No page to go back to
  98         else:
  99             if self.browser.can_go_back():
 100                  self.browser.go_back()
 101     def on_forward_clicked(self, widget=None, data=None):
 102         if sys.platform=="win32":
 103             try:
 104                  self.browser.GoForward()
 105             except:
 106                  pass
 107         else:
 108             if self.browser.can_go_forward():
 109                  self.browser.go_forward()
 110     def on_refresh_clicked(self, widget=None, data=None):
 111         if sys.platform=="win32":
 112             self.browser.Refresh()
 113         else:
 114             self.browser.reload(gtkmozembed.FLAG_RELOADNORMAL)
 115     def on_stop_clicked(self, widget=None, data=None):
 116         if sys.platform=="win32":
 117             self.browser.Stop()
 118         else:
 119             self.browser.stop_load()
 120     def on_home_clicked(self, widget=None, data=None):
 121         if sys.platform=="win32":
 122             # To go to Internet explorer's default home page use:
 123             #self.browser.GoHome()
 124             v = byref(VARIANT())
 125             self.browser.Navigate(self.home_url, v, v, v, v)
 126         else:
 127             self.browser.load_url(self.home_url)
 128     def on_go_clicked(self, widget=None, data=None):
 129         if sys.platform=="win32":
 130             v = byref(VARIANT())
 131             self.browser.Navigate(self.address.get_text(), v, v, v, v)
 132             #print dir(self.browser)
 133         else:
 134             self.browser.load_url(self.address.get_text())
 135     def on_address_keypress(self, widget, event):
 136         if gtk.gdk.keyval_name(event.keyval) == "Return":
 137             print "Key press: Return"
 138             self.on_go_clicked(None)
 139     def on_widget_click(self, widget, data):
 140         # used on win32 platform because by default a gtk application does
 141         # not grab control from native win32 control
 142         self.win.window.focus()
 143     def on_container_size(self, widget, sizeAlloc):
 144         self.gtkAtlAxWin.move_resize(0, 0, sizeAlloc.width, sizeAlloc.height)
 145     def on_container_focus(self, widget, data):
 146         # Used on win32 with Internet Explorer
 147         # Pass the focus to IE. First get the HWND of the IE control; this
 148         # is a bit of a hack but I couldn't make IWebBrowser2._get_HWND work.
 149         rect = RECT()
 150         user32.GetWindowRect(self.atlAxWinHwnd, byref(rect))
 151         ieHwnd = user32.WindowFromPoint(POINT(rect.left, rect.top))
 152         user32.SetFocus(ieHwnd)
 153 
 154 if __name__ == "__main__":
 155     gui = GUI()
 156     gtk.main()


反馈

创建 by -- ZoomQuiet [2009-10-16 14:06:18]

MiscItems/2009-10-16 (last edited 2009-12-25 07:11:04 by localhost)