HTML Widgets Documentation

Note

The API changed significantly in version 4. See the changelog for details.

The HtmlFrame widget is a Tkinter frame that provides additional functionality to the TkinterWeb widget by adding automatic scrollbars, error handling, and many convenience methods into one embeddable and easy to use widget.

The HtmlFrame widget is also capable managing other Tkinter widgets, making it easy to combine Tkinter widgets and HTML elements.

class tkinterweb.HtmlFrame(master, *, zoom=DEFAULT, fontscale=DEFAULT, messages_enabled=DEFAULT, vertical_scrollbar=DEFAULT, horizontal_scrollbar=DEFAULT, on_navigate_fail=DEFAULT, on_link_click=DEFAULT, on_form_submit=DEFAULT, on_script=DEFAULT, on_element_script=DEFAULT, on_resource_setup=DEFAULT, message_func=DEFAULT, request_func=DEFAULT, caret_browsing_enabled=DEFAULT, selection_enabled=DEFAULT, stylesheets_enabled=DEFAULT, images_enabled=DEFAULT, forms_enabled=DEFAULT, objects_enabled=DEFAULT, caches_enabled=DEFAULT, dark_theme_enabled=DEFAULT, image_inversion_enabled=DEFAULT, javascript_enabled=DEFAULT, javascript_backend=DEFAULT, events_enabled=DEFAULT, threading_enabled=DEFAULT, crash_prevention_enabled=DEFAULT, image_alternate_text_enabled=DEFAULT, ignore_invalid_images=DEFAULT, visited_links=DEFAULT, find_match_highlight_color=DEFAULT, find_match_text_color=DEFAULT, find_current_highlight_color=DEFAULT, find_current_text_color=DEFAULT, selected_text_highlight_color=DEFAULT, selected_text_color=DEFAULT, insecure_https=DEFAULT, ssl_cafile=DEFAULT, request_timeout=DEFAULT, headers=DEFAULT, experimental=DEFAULT, use_prebuilt_tkhtml=DEFAULT, tkhtml_version=DEFAULT, parsemode=DEFAULT, shrink=DEFAULT, textwrap=DEFAULT, mode=DEFAULT, defaultstyle=DEFAULT, height=DEFAULT, width=DEFAULT, **kwargs)

TkinterWeb’s flagship HTML widget.

Parameters:

master (tkinter.Widget) – The parent widget.

Callbacks:

Parameters:
  • on_navigate_fail (None or function) – The function to be called when a url cannot be loaded. The target url, error, and code will be passed as arguments. By default the TkinterWeb error page is shown.

  • on_link_click (None or function) – The function to be called when a hyperlink is clicked. The target url will be passed as an argument. By default the url is navigated to.

  • on_form_submit (None or function) – The function to be called when a form is submitted. The target url, data, and method (“GET” or “POST”) will be passed as arguments. By default the response is loaded.

  • on_script (None or function) – The function to be called when a <script> element is encountered. This can be used to connect a script handler, such as a JavaScript engine. The script element’s attributes and contents will be passed as arguments.

  • on_element_script (None or function) – The function to be called when a JavaScript event attribute on an element is encountered. This can be used to connect a script handler, such as a JavaScript engine, or even to run your own Python code. The element’s corresponding Tkhtml3 node, relevant attribute, and attribute contents will be passed as arguments. New in version 4.1.

  • on_resource_setup (None or function) – The function to be called when an image, stylesheet, or script load finishes. The resource’s url, type (“image”, “stylesheet”, or “script”), and whether setup was successful or not (True or False) will be passed as arguments.

Widget appearance:

Parameters:
  • visited_links (list) – The list used to determine if a hyperlink should be given the CSS :visited flag.

  • zoom (float) – The page zoom multiplier.

  • fontscale (float) – The page fontscale multiplier.

  • defaultstyle (str) – The default stylesheet to use when parsing HTML. Use caution when changing this setting. The default is tkintereb.utilities.DEFAULT_STYLE.

Widget sizing and overflow:

Parameters:
  • vertical_scrollbar (bool, "auto", or "dynamic") – Show the vertical scrollbar. You can also set the CSS overflow property on the <html> or <body> element instead.

  • horizontal_scrollbar (bool, "auto", or "dynamic") – Show the horizontal scrollbar. It is usually best to leave this hidden. You can also set the tkinterweb-overflow-x="scroll" | "auto" | "hidden" attribute on the <html> or <body> element instead.

  • shrink (bool) – If False, the widget’s width and height are set by the width and height options as per usual. If this option is set to True, the widget’s width and height are determined by the current document.

  • textwrap (bool or "auto") – Determines whether text is allowed to wrap. This is similar to the CSS text-wrap: normal | nowrap property, but more forceful. By default, wrapping will be disabled when shrink is True and will be enabled when shrink is False. Make sure the tkinterweb-tkhtml-extras package is installed; this is only partially supported in Tkhtml version 3.0. New in version 4.17.

Debugging:

Parameters:
  • messages_enabled (bool) – Enable/disable messages. Prior to version 4.25 this is enabled by default.

  • message_func (None or function) – The function to be called when a debug message is issued. Prior to version 4.25 this only works if messages are enabled. The message will be passed as an argument. If unset and enabled, by default the message is printed.

Features:

Parameters:
  • selection_enabled (bool) – Enable/disable selection. This is enabled by default.

  • caret_browsing_enabled (bool) – Enable/disable caret browsing. This is disabled by default. New in version 4.8.

  • stylesheets_enabled (bool) – Enable/disable stylesheets. This is enabled by default.

  • images_enabled (bool) – Enable/disable images. This is enabled by default.

  • forms_enabled (bool) – Enable/disable forms and form elements. This is enabled by default.

  • objects_enabled (bool) – Enable/disable embedding of <object> and <iframe> elements. This is enabled by default.

  • caches_enabled (bool) – Enable/disable caching. Disabling this option will conserve memory, but will also result in longer page and image reload times. This is enabled by default. Largely for debugging.

  • crash_prevention_enabled (bool) – Enable/disable crash prevention. In older Tkhtml versions, disabling this option may improve page load speed, but crashes will occur on some websites. This is enabled by default. Largely for debugging.

  • events_enabled (bool) – Enable/disable generation of Tk events. This is enabled by default. Largely for debugging.

  • threading_enabled (bool) – Enable/disable threading. Has no effect if the Tcl/Tk build does not support threading. This is enabled by default. Largely for debugging.

  • javascript_enabled (bool) – Enable/disable JavaScript support. This is disabled by default. Experimental. New in version 4.1.

  • javascript_backend ("pythonmonkey" or "python") – The scripting backend to use. Set to pythonmonkey (the default) to evaluate scripts as JavaScript code, or set to python to evaluate as Python code. Experimental. New in version 4.19.

  • image_alternate_text_enabled (bool) – Enable/disable the display of alt text for broken images. This is enabled by default.

  • dark_theme_enabled (bool) – Enable/disable dark mode. This feature is a work-in-progress and may cause hangs or crashes on more complex websites.

  • image_inversion_enabled (bool) – Enable/disable image inversion. If enabled, an algorithm will attempt to detect and invert images with a predominantly light-coloured background. Photographs and dark-coloured images should be left as is. This feature is a work-in-progress and may cause hangs or crashes on more complex websites.

  • ignore_invalid_images (bool) – If enabled and alt text is disabled or the image has no alt text, a broken image icon will be displayed in place of the image.

Widget colours and styling:

Parameters:
  • find_match_highlight_color (str) – The background colour of matches found by find_text().

  • find_match_text_color (str) – The foreground colour of matches found by find_text().

  • find_current_highlight_color (str) – The background colour of the current match selected by find_text().

  • find_current_text_color (str) – The foreground colour of the current match selected by find_text().

  • selected_text_highlight_color (str) – The background colour of selected text.

  • selected_text_color (str) – The foreground colour of selected text.

Download behaviour:

Parameters:
  • insecure_https (bool) – If True, website certificate errors are ignored. This can be used to work around issues where ssl is unable to get a page’s certificate on some older Mac systems.

  • ssl_cafile (None or str) – Path to a file containing CA certificates. This can be used to work around issues where ssl is unable to get a page’s certificate on some older Mac systems. New in version 4.5.

  • headers (dict) – The headers used by urllib’s Request when fetching a resource.

  • request_timeout (int) – The number of seconds to wait when fetching a resource before timing out. New in version 4.6.

  • request_func (None or function) – The function to be called when a resource is requested. This overrides all other download settings. The callback must accept the following arguments: the resource’s url, data, method (“GET” or “POST”), and encoding. The callback must return the following: url, data, file type, and HTTP code.

HTML rendering behaviour:

Parameters:
  • experimental (bool or "auto") – If True, experimental features will be enabled. If “auto”, experimental features will be enabled if the loaded Tkhtml version supports experimental features. You will need to compile the cutting-edge Tkhtml widget from https://github.com/Andereoo/TkinterWeb-Tkhtml/tree/experimental and replace the default Tkhtml binary for your system with the experimental version. Unless you need to screenshot the full page on Windows or print your page for now it is likely best to use the default Tkhtml binary and leave this setting alone.

  • use_prebuilt_tkhtml (bool) – If True (the default), the Tkhtml binary for your system supplied by TkinterWeb will be used. If your system isn’t supported and you don’t want to compile the Tkhtml widget from https://github.com/Andereoo/TkinterWeb-Tkhtml yourself, you could try installing Tkhtml3 system-wide and set use_prebuilt_tkhtml to False. Note that some crash prevention features will no longer work.

  • tkhtml_version (float or "auto") – The Tkhtml version to use. If the requested version is not found, TkinterWeb will fallback to Tkhtml 3.0. Only one Tkhtml version can be loaded at a time. New in version 4.4.

  • parsemode ("xml", "xhtml", or "html") – The parse mode. In “html” mode, explicit XML-style self-closing tags are not handled specially and unknown tags are ignored. “xhtml” mode is similar to “html” mode except that explicit self-closing tags are recognized. “xml” mode is similar to “xhtml” mode except that XML CDATA sections and unknown tag names are recognized. It is usually best to leave this setting alone.

  • mode ("standards", "almost standards", or "quirks") – The rendering engine mode. It is usually best to leave this setting alone.

Other ttk.Frame arguments, such as width, height, and style are also supported.

Raises:

TypeError – If the value type is wrong and cannot be converted to the correct type.

property title

The document’s title.

Return type:

str

property icon

The document icon’s url.

Return type:

str

property current_url

The document’s url.

Return type:

str

property base_url

The documents’s base url. This is automatically generated from but will also change if explicitly specified by the document.

Return type:

str

property document

The DOM manager. Use this to access HTMLDocument methods to manupulate the DOM.

Return type:

HTMLDocument

property javascript

The JavaScript manager. Use this to access JSEngine methods.

Return type:

JSEngine

property html

The underlying html widget. Use this to access internal TkinterWeb methods.

Return type:

TkinterWeb

load_html(html_source, base_url=None, fragment=None)

Clear the current page and parse the given HTML code.

Parameters:
  • html_source (str) – The HTML code to render.

  • base_url (str, optional) – The base url to use when parsing stylesheets and images. If this argument is not supplied, it will be set to the current working directory.

  • fragment (str, optional) – The url fragment to scroll to after the document loads.

load_file(file_url, decode=None, force=False)

Convenience method to load a local HTML file.

This method will always load the file in the main thread. If you want to load the file in a seperate thread, use HtmlFrame.load_url().

Parameters:
  • file_url (str) – The HTML file to render.

  • decode (str or None, optional) – The decoding to use when loading the file.

  • force (bool, optional) – Force the page to reload all elements.

load_website(website_url, decode=None, force=False)

Convenience method to load a website.

Parameters:
  • website_url (str) – The url to load.

  • decode (str or None, optional) – The decoding to use when loading the website.

  • force (bool, optional) – Force the page to reload all elements.

load_url(url, decode=None, force=False)

Loads and renders HTML from the given url.

A local file will be loaded if the url begins with “file://”. A website will be loaded if the url begins with “https://” or “http://”. If the url begins with “view-source:”, the source code of the webpage will be displayed. Loading “about:tkinterweb” will open a page with debugging information.

Parameters:
  • url (str) – The url to load.

  • decode (str or None, optional) – The decoding to use when loading the url.

  • force (bool, optional) – Force the page to reload all elements.

load_form_data(url, data, method='GET', decode=None, force=False)

Submit form data to a server and load the response.

Parameters:
  • url (str) – The url to load.

  • data (str) – The data to pass to the server.

  • method ("GET" or "POST", optional) – The form submission method.

  • decode (str or None, optional) – The decoding to use when loading the file.

  • force (bool, optional) – Force the page to reload all elements. New in version 4.24.

reload()

Reload the page. This only affects pages loaded from a url.

New in version 4.21

add_html(html_source, return_element=False, index=-1)

Parse HTML and add it to the end of the current document. Unlike HtmlFrame.load_html(), HtmlFrame.add_html() adds rendered HTML code without clearing the original document.

Parameters:
  • html_source (str) – The HTML code to render.

  • return_element (bool, optional) – If True, return the root element of the added HTML.

  • index (int, optional) – The index of the element to insert before. Default -1. New in version 4.22.

Returns:

HTMLElement or None

add_css(css_source, priority='author')

Send CSS stylesheets to the parser. This can be used to alter the appearance of already-loaded documents.

Parameters:
  • css_source (str) – The CSS code to parse.

  • priority ("agent", "user", or "author") – The priority of the CSS code. CSS code loaded by webpages is “user” priority. “agent” is lower priority than “user” and “author” (the default) is higher .

import_css(url)

Add a CSS stylesheet given a url.

Parameters:

url (str) – The url of the CSS stylesheet.

New in version 4.19.

stop()

Stop loading this page and abandon all pending requests.

find_text(text, select=1, ignore_case=True, highlight_all=True, detailed=False)

Search the document for text and highlight matches.

Parameters:
  • text (str) – The Regex expression to use to find text. If this is set to a blank string (“”), all highlighted text will be cleared.

  • select (int, optional) – The index of the match to select and scroll to. Use this to implement find next/find previous functionality.

  • ignore_case (bool, optional) – If True, uppercase and lowercase letters will be treated as the same character.

  • highlight_all (bool, optional) – If True, all matches will be highlighted.

  • detailed (bool, optional) – If True, this method will also return information on the nodes that were found. See bug #93 for more details.

Returns:

The number of matches.

Return type:

int

widget_to_element(widget)

Get the HTML element containing the given widget.

Parameters:

widget (tkinter.Widget) – The widget to search for.

Returns:

The element containing the given widget.

Return type:

HTMLElement

Raises:

KeyError – If the given widget is not in the document.

New in version 4.2.

screenshot_page(filename=None, full=False, show=False)

Take a screenshot.

This command should be used with care on large documents if full is set to True, as it may generate very large images that take a long time to create and consume large amounts of memory.

On Windows, if experimental mode is not enabled, ensure you run ctypes.windll.shcore.SetProcessDpiAwareness(1) before creating your Tkinter window or else the screenshot may be badly offset. On Windows it’s good practice to run this anyway.

Parameters:
  • filename (str or None, optional) – The file path to save the screenshot to. If None, the image is not saved to the disk.

  • full (bool, optional) – If True, the entire page is captured. On Windows, experimental mode must be enabled. If False, only the visible content is captured.

  • show (bool, optional) – Display the screenshot in the default system handler.

Returns:

A PIL Image containing the rendered document.

Return type:

PIL.Image

Raises:

NotImplementedError – If experimental mode is not enabled, full is set to True, and TkinterWeb is running on Windows.

print_page(filename=None, cnf={}, **kwargs)

Print the document to a PostScript file.

This method is experimental and requires experimental mode to be enabled.

Parameters:
  • filename (str or None, optional) – The file path to print the page to. If None, the image is not saved to the disk.

  • kwargs – Other valid options are colormap, colormode, file, fontmap, height, pageanchor, pageheight, pagesize (can be A3, A4, A5, LEGAL, and LETTER), pagewidth, pagex, pagey, nobg, noimages, rotate, width, x, and y.

Returns:

A string containing the PostScript code.

Return type:

str

Raises:

NotImplementedError – If experimental mode is not enabled.

save_page(filename=None)

Return the page’s HTML code or save the page as an HTML file.

As of version 4.23, if caching is enabled and a url is loaded, this method returns the page’s original HTML. Otherwise, the returned page will be the output of HtmlFrame.snapshot_page(), with the contents of the <head> tag included if the widget is still loading.

Parameters:

filename (str or None, optional) – The file path to save the page to. If None, the page is not saved to the disk.

Returns:

A string containing the page’s HTML/CSS code.

Return type:

str

snapshot_page(filename=None, allow_agent=False, include_head=False)

Save a snapshot of the document.

Unlike save_page(), which returns the original document, snapshot_page() returns the page as rendered. By default <link> elements are ignored and instead one <style> element contains all of the necessary CSS information for the document. This can be useful for saving documents for offline use.

Parameters:
  • filename (str or None, optional) – The file path to save the page to. If None, the page is not saved to the disk.

  • allow_agent (bool, optional) – If True, CSS properties added by the rendering engine (eg. those affected by the widget’s default_style option) are also included.

  • include_head (bool, optional) – If True, the contents of the page’s <head> element (i.e. <link> and <meta> tags) are included. Default False. New in version 4.23.

Returns:

A string containing the page’s rendered HTML/CSS code.

Return type:

str

get_page_text()

Return the page’s text content.

Returns:

A string containing the page’s text content.

Return type:

str

New in version 4.8.

show_error_page(url, error, code)

Show the error page.

Parameters:
  • url (str) – The url of the broken page.

  • error (str) – The error message.

  • code (str) – The HTTP error code.

resolve_url(url)

Generate a full url from the specified url. This can be used to generate full urls when given a relative url.

Parameters:

url (str) – The url to modify if needed.

Returns:

The full, resolved url.

Return type:

str

yview(*args)

Adjust the viewport.

This method uses the standard interface copied from other built-in scrollable Tkinter widgets. Additionally, if a Tkhtml3 node is supplied as an argument, the document will scroll to the top of the given node.

yview_moveto(number)

Moves the view vertically to the specified position.

Parameters:

number (float) – The position to scroll to.

yview_scroll(number, what)

Shifts the view in up or down.

Parameters:
  • number (int) – Specifies the number of ‘whats’ to scroll by; make positive to scroll down or negative to scroll up.

  • what (str) – Either “units” or “pages”

get_currently_hovered_element(ignore_text_nodes=True)

Get the element under the mouse. Particularly useful for creating right-click menus or displaying hints when the mouse moves.

Parameters:

ignore_text_nodes (bool, optional) – If True, text nodes (i.e. the contents of a <p> element) will be ignored and their parent node returned. It is generally best to leave leave this at the default.

Returns:

The element under the mouse.

Return type:

HTMLElement or None

get_caret_position(return_element=True)

Get the position of the caret. This can be used to modify the document’s text when the user types.

Parameters:

return_element (bool, optional) – If True, this method will also return information on the nodes that were found.

If return_element=True:

return:

The HTMLElement under the caret, the element’s textContent, and an index representing the position in that string that the caret is at. If the caret is not visible, this method will return None.

rtype:

HTMLElement, str, and int, or None

The element returned will always be a text node. If you need to change the style or HTML content of a text node you will first need to get its parent.

If return_element=False:

return:

The text content of the page, and an index representing the position in that string that the caret is at. If the caret is not visible, this method will return None.

rtype:

str and int, or None

Changed in version 4.16.

set_caret_position(element=None, index=0)

Set the position of the caret, given an index and, optionally, an HTML element.

If the given index extends out of the bounds of the given element, the caret will be moved into the preceeding or following elements.

Parameters:
  • element – Specifies the element to place the caret in. This element must be a text node, must contain text, and must be visible.

  • index (int) – The index in the element’s textContent to place the caret at. If element is None, the index will be used relative to the page’s text content.

Raises:

RuntimeError – If caret browsing is disabled or the given element is empty or has been removed.

Changed in version 4.16.

shift_caret_left()

Shift the caret left. If the caret is at the beginning of a node, this method will move the caret to the end of the previous text node.

Raises:

RuntimeError – If caret browsing is disabled.

New in version 4.8.

shift_caret_right()

Shift the caret right. If the caret is at the end of a node, this method will move the caret to the beginning of the next text node.

Raises:

RuntimeError – If caret browsing is disabled.

New in version 4.8.

get_selection_position(return_elements=True)

Get the start position, end position, and, optionally, contained elements of selected text.

Parameters:

return_elements (bool, optional) – If True, this method will also return information on the nodes that were found.

If return_elements=True:

return:
  • A tuple containing:
    • The HTMLElement containing the start of the selection

    • The text content of that element

    • An index representing the position in that string that the selection begins at

  • A second tuple containing:
    • The HTMLElement containing the end of the selection

    • The text content of that element

    • An index representing the position in that string that the selection ends at

  • A list containing an HTMLElement for each other element under the selection, in sequencial order.

rtype:

A pair of (HTMLElement, str, int) tuples and a list of HTMLElement objects, or None

The elements returned will always be text nodes. If you need to change the style or HTML content of a text node you will first need to get its parent.

If return_elements=False:

return:

The document’s text, and two indexes representing the selection’s start and end positions in that string.

rtype:

str, int, and int, or None

If no selection is found, this method will return None

Changed in version 4.16.

set_selection_position(start_element=None, start_index=0, end_element=None, end_index=0)

Set the current selection, given starting and ending text indexes and, optionally, HTML elements.

Parameters:
  • start_element – Specifies the element to begin the selection in. This element must be text nodes, must contain text, and must be visible.

  • start_index (int) – The index in the element’s textContent to begin the selection at. If start_element is None, this index instead is relative to the page’s text content.

  • end_element – Specifies the element to end the selection in. This element must be text nodes, must contain text, and must be visible.

  • end_index (int) – The index in the element’s textContent to end the selection at. If end_element is None, this index instead is relative to the page’s text content.

Raises:

RuntimeError – If selection is disabled or the given elements are empty or have been removed.

Changed in version 4.16.

get_selection()

Return any selected text.

Returns:

The current selection.

Return type:

str

clear_selection()

Clear the current selection.

select_all()

Select all text in the document.

generate_style_report(return_report=False)

Return or load a window showing this widget’s style report.

Parameters:

return_report (str, optional) – If False, a window opens showing the report. If True, the report is simply returned.

New in version 4.19.

configure(**kwargs)

Change the widget’s configuration options. See above for options.

config(_override=False, **kwargs)

Change the widget’s configuration options. See above for options.

cget(key)

Return the value of a given configuration option. See above for options.

bind(sequence, *args, **kwargs)

Add an event binding. For convenience, some bindings will be bound to this widget and others will be bound to its associated TkinterWeb instance.

unbind(sequence, *args, **kwargs)

Remove an event binding.

This widget also emits the following Tkinter virtual events that can be bound to:

  • <<DownloadingResource>>/utilities.DOWNLOADING_RESOURCE_EVENT: Generated whenever a new resource is being downloaded.

  • <<DoneLoading>>/utilities.DONE_LOADING_EVENT: Generated whenever all outstanding resources have been downloaded. This is generally a good indicator as to when the website is done loading, but may be generated multiple times while loading a page.

  • <<DOMContentLoaded>>/utilities.DOM_CONTENT_LOADED_EVENT: Generated once the page content has loaded. The page may not be done loading, but at this point it is possible to interact with the DOM.

  • <<UrlChanged>>/utilities.URL_CHANGED_EVENT: Generated whenever the widget’s url changes or redirects. Use HtmlFrame.current_url to get the url.

  • <<IconChanged>>/utilities.ICON_CHANGED_EVENT: Generated whenever the icon of a webpage changes. Use HtmlFrame.icon to get the icon.

  • <<TitleChanged>>/utilities.TITLE_CHANGED_EVENT: Generated whenever the title of a website or file has changed. Use HtmlFrame.title to get the title.

  • <<Modified>>`/utilities.FIELD_CHANGED_EVENT: Generated whenever the content of an interactive element changes.

class tkinterweb.HtmlLabel(master, *, text='', **kwargs)

The HtmlLabel widget is a label-like HTML widget. It inherits from the HtmlFrame class.

For a complete list of avaliable methods, properties, configuration options, and generated events, see the HtmlFrame docs.

This widget also accepts one additional parameter:

Parameters:

text (str) – The HTML or text content of the widget

By default the widget will be styled to match the ttk.Label style. To change this, alter the ttk style or use CSS.

class tkinterweb.HtmlText(master, *, background='#ffffff', foreground='#000000', selectbackground='#9bc6fa', selectforeground='#000', insertontime=600, insertofftime=300, insertwidth=1, insertbackground=None, state='enabled', **kwargs)

The HtmlText widget is a text-like HTML widget. It inherits from the HtmlFrame class.

For a complete list of avaliable methods, properties, configuration options, and generated events, see the HtmlFrame docs.

The intent of this widget is to mimic the Tkinter Text widget.

This widget accepts the following tk.Text parameters:

Parameters:
  • background (str) – the widget’s background colour

  • foreground (str) – the widget’s foreground (text) colour

  • selectbackground (str) – the background colour of selected text

  • selectforeground (str) – the foreground colour of selected text

  • insertontime (int) – the number of milliseconds the insertion cursor is visible

  • insertofftime (int) – the number of milliseconds the insertion cursor is invisible

  • insertwidth (int) – the width of the insertion cursor in pixels

  • insertbackground (str) – the background colour of the insertion cursor

  • state (str) – the widget’s state ("normal" or "disabled")

Changed in version 4.15.

delete(start_index, end_index=None, start_element=None, end_element=None)

Delete text between two points.

Parameters:
  • start_index (int or "sel.first") – The starting index to delete text from. Similar to the tk.Text widget, if this is “sel.first”, this will be the beginning of the selection.

  • end_index (int, "sel.last", "end", or None, optional) – The ending index to delete text at. If this is “sel.last”, this will be the end of the selection. If this is “end”, this will be the end of the document or the end of the ending element if provided. If this is None, only the character at the start index will be deleted.

  • start_element (None or HTMLElement, optional) – If provided, the start index will be used relative to this element’s textContent.

  • end_element (None or HTMLElement, optional) – If provided, the end index will be used relative to this element’s textContent.

Raises:

RuntimeError – if “sel.first” or “self.last” is requested but no text is selected.

New in version 4.16.

insert(index, text_or_element, element=None)

Insert text or an element at the given index.

Parameters:
  • index (int, "end", or "insert") – The index to insert text at. Similar to the tk.Text widget, if this is “insert”, this method will insert at the caret’s position. If this is “end”, this will insert at the end of the document or the end of the element if provided.

  • text_or_element (str or HTMLElement) – The text or HTML element to insert.

  • element (None or HTMLElement, optional) – If provided, the given index will be used relative to this element’s textContent.

Raises:

RuntimeError – if “insert” is requested but the caret is not visible.

New in version 4.16.

class tkinterweb.HtmlParse(**kwargs)

The HtmlParse class parses HTML but does not spawn a widget. It inherits from the HtmlFrame class.

For a complete list of avaliable methods, properties, configuration options, and generated events, see the HtmlFrame docs.

New in version 4.4.