This control may be used to render web (HTML / CSS / javascript) documents.
It is designed to allow the creation of multiple backends for each port, although currently just one is available. It differs from wx.html.HtmlWindow in that each backend is actually a full rendering engine, Trident on MSW and Webkit on OSX and GTK. This allows the correct viewing complex pages with javascript and css.
WEBVIEW_BACKEND_IE
(MSW)¶The IE backend uses Microsoft’s Trident rendering engine, specifically the version used by the locally installed copy of Internet Explorer. As such it is only available for the MSW port. By default recent versions of the
WebBrowser control, which this backend uses, emulate Internet Explorer 7. This can be changed with a registry setting, see this article for more information. This backend has full support for custom schemes and virtual file systems.
WEBVIEW_WEBKIT
(GTK)¶Under GTK the WebKit backend uses
WebKitGTK+. The current minimum version required is 1.3.1 which ships by default with Ubuntu Natty and Debian Wheezy and has the package name libwebkitgtk-dev. Custom schemes and virtual files systems are supported under this backend, however embedded resources such as images and stylesheets are currently loaded using the data:// scheme.
Many of the methods in wx.html2.WebView are asynchronous, i.e. they return immediately and perform their work in the background. This includes functions such as LoadURL
and Reload
. To receive notification of the progress and completion of these functions you need to handle the events that are provided. Specifically wxEVT_WEBVIEW_LOADED
notifies when the page or a sub-frame has finished loading and wxEVT_WEBVIEW_ERROR
notifies that an error has occurred.
wx.html2.WebView supports the registering of custom scheme handlers, for example file
or http
. To do this create a new class which inherits from wx.html2.WebViewHandler, where WebHandler.GetFile() returns a pointer to a wx.FSFile which represents the given url. You can then register your handler with RegisterHandler
it will be called for all pages and resources. wx.html2.WebViewFSHandler is provided to access the virtual file system encapsulated by wx.FileSystem. The wx.MemoryFSHandler documentation gives an example of how this may be used. wx.html2.WebViewArchiveHandler is provided to allow the navigation of pages inside a zip archive. It supports paths of the form: scheme:///C
:/example/docs.zip;protocol=zip/main.htm
Handlers bound for the following event types will receive one of the wx.html2.WebViewEvent parameters.
wxEVT_WEBVIEW_NAVIGATING
event, generated before trying to get a resource. This event may be vetoed to prevent navigating to this resource. Note that if the displayed HTML document has several frames, one such event will be generated per frame.wxEVT_WEBVIEW_NAVIGATED
event generated after it was confirmed that a resource would be requested. This event may not be vetoed. Note that if the displayed HTML document has several frames, one such event will be generated per frame.wxEVT_WEBVIEW_LOADED
event generated when the document is fully loaded and displayed. Note that if the displayed HTML document has several frames, one such event will be generated per frame.wxEVT_WEBVIEW_ERROR
event generated when a navigation error occurs. The integer associated with this event will be a WebNavigationError item. The string associated with this event may contain a backend-specific more precise error message/code.wxEVT_WEBVIEW_NEWWINDOW
event, generated when a new window is created. You must handle this event if you want anything to happen, for example to load the page in a new window or tab.wxEVT_WEBVIEW_TITLE_CHANGED
event, generated when the page title changes. Use GetString to get the title.New in version 2.9.3.
See also
CanCopy |
Returns True if the current selection can be copied. |
CanCut |
Returns True if the current selection can be cut. |
CanGoBack |
Returns True if it is possible to navigate backward in the history of visited pages. |
CanGoForward |
Returns True if it is possible to navigate forward in the history of visited pages. |
CanPaste |
Returns True if data can be pasted. |
CanRedo |
Returns True if there is an action to redo. |
CanSetZoomType |
Retrieve whether the current HTML engine supports a zoom type. |
CanUndo |
Returns True if there is an action to undo. |
ClearHistory |
Clear the history, this will also remove the visible page. |
ClearSelection |
Clears the current selection. |
Copy |
Copies the current selection. |
Create |
Creation function for two-step creation. |
Cut |
Cuts the current selection. |
DeleteSelection |
Deletes the current selection. |
EnableContextMenu |
Enable or disable the right click context menu. |
EnableHistory |
Enable or disable the history. |
Find |
Finds a phrase on the current page and if found, the control will scroll the phrase into view and select it. |
GetBackwardHistory |
Returns a list of items in the back history. |
GetCurrentTitle |
Get the title of the current web page, or its URL/path if title is not available. |
GetCurrentURL |
Get the URL of the currently displayed document. |
GetForwardHistory |
Returns a list of items in the forward history. |
GetNativeBackend |
Return the pointer to the native backend used by this control. |
GetPageSource |
Get the HTML source code of the currently displayed document. |
GetPageText |
Get the text of the current page. |
GetSelectedSource |
Returns the currently selected source, if any. |
GetSelectedText |
Returns the currently selected text, if any. |
GetZoom |
Get the zoom factor of the page. |
GetZoomType |
Get how the zoom factor is currently interpreted. |
GoBack |
Navigate back in the history of visited pages. |
GoForward |
Navigate forward in the history of visited pages. |
HasSelection |
Returns True if there is a current selection. |
IsBusy |
Returns whether the web control is currently busy (e.g. loading a page). |
IsContextMenuEnabled |
Returns True if a context menu will be shown on right click. |
IsEditable |
Returns whether the web control is currently editable. |
LoadURL |
Load a web page from a URL. |
New |
Factory function to create a new wx.html2.WebView with two-step creation, wx.html2.WebView.Create should be called on the returned object. |
Paste |
Pastes the current data. |
Print |
Opens a print dialog so that the user may print the currently displayed page. |
Redo |
Redos the last action. |
RegisterFactory |
Allows the registering of new backend for wx.html2.WebView. |
RegisterHandler |
Registers a custom scheme handler. |
Reload |
Reload the currently displayed URL. |
RunScript |
Runs the given javascript code. |
SelectAll |
Selects the entire page. |
SetEditable |
Set the editable property of the web control. |
SetPage |
Set the displayed page source to the contents of the given string. |
SetZoom |
Set the zoom factor of the page. |
SetZoomType |
Set how to interpret the zoom factor. |
Stop |
Stop the current page loading process, if any. |
Undo |
Undos the last action. |
BackwardHistory |
See GetBackwardHistory |
CurrentTitle |
See GetCurrentTitle |
CurrentURL |
See GetCurrentURL |
ForwardHistory |
See GetForwardHistory |
NativeBackend |
See GetNativeBackend |
PageSource |
See GetPageSource |
PageText |
See GetPageText |
SelectedSource |
See GetSelectedSource |
SelectedText |
See GetSelectedText |
Zoom |
See GetZoom and SetZoom |
ZoomType |
See GetZoomType and SetZoomType |
wx.html2.
WebView
(Control)¶This control may be used to render web (HTML / CSS / javascript) documents.
CanCopy
(self)¶Returns True
if the current selection can be copied.
Return type: | bool |
---|
Note
This always returns true
on the OSX WebKit backend.
CanCut
(self)¶Returns True
if the current selection can be cut.
Return type: | bool |
---|
Note
This always returns true
on the OSX WebKit backend.
CanGoBack
(self)¶Returns True
if it is possible to navigate backward in the history of visited pages.
Return type: | bool |
---|
CanGoForward
(self)¶Returns True
if it is possible to navigate forward in the history of visited pages.
Return type: | bool |
---|
CanPaste
(self)¶Returns True
if data can be pasted.
Return type: | bool |
---|
Note
This always returns true
on the OSX WebKit backend.
CanRedo
(self)¶Returns True
if there is an action to redo.
Return type: | bool |
---|
CanSetZoomType
(self, type)¶Retrieve whether the current HTML engine supports a zoom type.
Parameters: | type (WebViewZoomType) – The zoom type to test. |
---|---|
Return type: | bool |
Returns: | Whether this type of zoom is supported by this HTML engine (and thus can be set through SetZoomType ). |
CanUndo
(self)¶Returns True
if there is an action to undo.
Return type: | bool |
---|
ClearHistory
(self)¶Clear the history, this will also remove the visible page.
ClearSelection
(self)¶Clears the current selection.
Copy
(self)¶Copies the current selection.
Create
(self, parent, id=ID_ANY, url=WebViewDefaultURLStr, pos=DefaultPosition, size=DefaultSize, style=0, name=WebViewNameStr)¶Creation function for two-step creation.
Parameters: | |
---|---|
Return type: | bool |
Cut
(self)¶Cuts the current selection.
DeleteSelection
(self)¶Deletes the current selection.
Note that for WEBVIEW_BACKEND_WEBKIT
the selection must be editable, either through SetEditable or the correct HTML attribute.
EnableContextMenu
(self, enable=True)¶Enable or disable the right click context menu.
By default the standard context menu is enabled, this method can be used to disable it or re-enable it later.
Parameters: | enable (bool) – |
---|
New in version 2.9.5.
EnableHistory
(self, enable=True)¶Enable or disable the history.
This will also clear the history.
Parameters: | enable (bool) – |
---|
Find
(self, text, flags=WEBVIEW_FIND_DEFAULT)¶Finds a phrase on the current page and if found, the control will scroll the phrase into view and select it.
Parameters: |
|
---|---|
Return type: | long |
Returns: | If search phrase was not found in combination with the flags then |
New in version 2.9.5.
Note
This function will restart the search if the flags WEBVIEW_FIND_ENTIRE_WORD
or WEBVIEW_FIND_MATCH_CASE
are changed, since this will require a new search. To reset the search, for example resetting the highlights call the function with an empty search phrase. This always returns NOT_FOUND
on the OSX WebKit backend.
GetBackwardHistory
(self)¶Returns a list of items in the back history.
The first item in the vector is the first page that was loaded by the control.
Return type: | PyObject |
---|
GetCurrentTitle
(self)¶Get the title of the current web page, or its URL/path if title is not available.
Return type: | string |
---|
GetCurrentURL
(self)¶Get the URL of the currently displayed document.
Return type: | string |
---|
GetForwardHistory
(self)¶Returns a list of items in the forward history.
The first item in the vector is the next item in the history with respect to the currently loaded page.
Return type: | PyObject |
---|
GetNativeBackend
(self)¶Return the pointer to the native backend used by this control.
This method can be used to retrieve the pointer to the native rendering engine used by this control. The return value needs to be down-casted to the appropriate type depending on the platform: under Windows, it’s a pointer to IWebBrowser2 interface, under OS X it’s a WebView pointer and under GTK it’s a WebKitWebView.
For example, you could set the WebKit options using this method:
# In Python the value returned will be a sip wrapper around a void* type,
# and it can be converted to the address being pointed to with int().
webview_ptr = self.webview.GetNativeBackend()
# Assuming you are able to get a ctypes, cffi or similar access to the
# webview library, you can use that pointer value to give it access to the
# WebView backend to operate upon.
theWebViewLib.doSomething(int(webview_ptr))
New in version 2.9.5.
GetPageSource
(self)¶Get the HTML source code of the currently displayed document.
Return type: | string |
---|---|
Returns: | The HTML source code, or an empty string if no page is currently shown. |
GetPageText
(self)¶Get the text of the current page.
Return type: | string |
---|
GetSelectedSource
(self)¶Returns the currently selected source, if any.
Return type: | string |
---|
GetSelectedText
(self)¶Returns the currently selected text, if any.
Return type: | string |
---|
GetZoom
(self)¶Get the zoom factor of the page.
Return type: | wx.html2.WebViewZoom |
---|---|
Returns: | The current level of zoom. |
GetZoomType
(self)¶Get how the zoom factor is currently interpreted.
Return type: | wx.html2.WebViewZoomType |
---|---|
Returns: | How the zoom factor is currently interpreted by the HTML engine. |
GoForward
(self)¶Navigate forward in the history of visited pages.
Only valid if CanGoForward
returns True
.
HasSelection
(self)¶Returns True
if there is a current selection.
Return type: | bool |
---|
IsBusy
(self)¶Returns whether the web control is currently busy (e.g. loading a page).
Return type: | bool |
---|
IsContextMenuEnabled
(self)¶Returns True
if a context menu will be shown on right click.
Return type: | bool |
---|
New in version 2.9.5.
IsEditable
(self)¶Returns whether the web control is currently editable.
Return type: | bool |
---|
LoadURL
(self, url)¶Load a web page from a URL.
Parameters: | url (string) – The URL of the page to be loaded. |
---|
Note
Web engines generally report errors asynchronously, so if you wish to know whether loading the URL was successful, register to receive navigation error events.
New
(*args, **kw)¶New (backend=WebViewBackendDefault)
Factory function to create a new wx.html2.WebView with two-step creation, wx.html2.WebView.Create
should be called on the returned object.
Parameters: | backend (string) – The backend web rendering engine to use. WebViewBackendDefault , WebViewBackendIE and WebViewBackendWebKit are predefined where appropriate. |
---|---|
Return type: | wx.html2.WebView |
Returns: | The created wx.html2.WebView |
New in version 2.9.5.
New (parent, id=ID_ANY, url=WebViewDefaultURLStr, pos=DefaultPosition, size=DefaultSize, backend=WebViewBackendDefault, style=0, name=WebViewNameStr)
Factory function to create a new wx.html2.WebView using a wx.html2.WebViewFactory.
Parameters: |
|
---|---|
Return type: | |
Returns: | The created wx.html2.WebView, or |
New in version 2.9.5.
Paste
(self)¶Pastes the current data.
Print
(self)¶Opens a print dialog so that the user may print the currently displayed page.
Redo
(self)¶Redos the last action.
RegisterFactory
(backend, factory)¶Allows the registering of new backend for wx.html2.WebView.
backend can be used as an argument to New
.
Parameters: |
|
---|
New in version 2.9.5.
RegisterHandler
(self, handler)¶Registers a custom scheme handler.
Parameters: | handler (wx.html2.WebViewHandler) – A shared pointer to a WebHandler. |
---|
Reload
(self, flags=WEBVIEW_RELOAD_DEFAULT)¶Reload the currently displayed URL.
Parameters: | flags (WebViewReloadFlags) – A bit array that may optionally contain reload options. |
---|
RunScript
(self, javascript)¶Runs the given javascript code.
Parameters: | javascript (string) – |
---|
Note
When using WEBVIEW_BACKEND_IE
you must wait for the current page to finish loading before calling RunScript
.
SelectAll
(self)¶Selects the entire page.
SetEditable
(self, enable=True)¶Set the editable property of the web control.
Enabling allows the user to edit the page even if the contenteditable
attribute is not set. The exact capabilities vary with the backend being used.
Parameters: | enable (bool) – |
---|
SetPage
(self, *args, **kw)¶SetPage (self, html, baseUrl)
Set the displayed page source to the contents of the given string.
Parameters: |
|
---|
Note
When using WEBVIEW_BACKEND_IE
you must wait for the current page to finish loading before calling SetPage
. The baseURL parameter is not used in this backend.
SetPage (self, html, baseUrl)
Set the displayed page source to the contents of the given stream.
Parameters: |
|
---|
SetZoom
(self, zoom)¶Set the zoom factor of the page.
Parameters: | zoom (WebViewZoom) – How much to zoom (scale) the HTML document. |
---|
SetZoomType
(self, zoomType)¶Set how to interpret the zoom factor.
Parameters: | zoomType (WebViewZoomType) – How the zoom factor should be interpreted by the HTML engine. |
---|
Note
invoke CanSetZoomType
first, some HTML renderers may not support all zoom types.
Stop
(self)¶Stop the current page loading process, if any.
May trigger an error event of type WEBVIEW_NAV_ERR_USER_CANCELLED
. TODO
: make WEBVIEW_NAV_ERR_USER_CANCELLED
errors uniform across ports.
Undo
(self)¶Undos the last action.
BackwardHistory
¶CurrentTitle
¶See GetCurrentTitle
CurrentURL
¶See GetCurrentURL
ForwardHistory
¶NativeBackend
¶See GetNativeBackend
PageSource
¶See GetPageSource
PageText
¶See GetPageText
SelectedSource
¶SelectedText
¶See GetSelectedText
ZoomType
¶See GetZoomType
and SetZoomType