Document Object Model Documentation
Note
The API changed significantly in version 4. See the changelog for details.
The methods described in this page make it easy to modify the appearance and content of a loaded document and manage interaction with the document. For the most part, this page mirrors the core JavaScript DOM API.
- class tkinterweb.dom.HTMLDocument(html)
Access this class via the
documentproperty of theHtmlFrameandHtmlLabelwidgets.- Variables:
html – The associated
TkinterWebinstance.
- property body
The document body element.
- Return type:
- property documentElement
The document root element.
- Return type:
- write(*text)
Write into the current document or output stream. If the document is loaded, this first deletes all existing HTML.
- Parameters:
text (str) – The text or HTML code to insert.
New in version 4.20.
- createElement(tagname)
Create and return a new HTML element with the given tag name.
- Parameters:
tagname (str) – The new element’s HTML tag.
- Return type:
- createTextNode(text)
Create and return a new text node with the given text content.
- Parameters:
text (str) – The text content of the new node.
- Return type:
- getElementById(query, _root=None)
Return an element that matches a given id.
- Parameters:
query (str) – The element id to be searched for.
- Return type:
- Raises:
tkinter.TclError
- getElementsByClassName(query, _root=None)
Return all elements that match a given class name.
- Parameters:
query (str) – The class to be searched for.
- Return type:
- Raises:
tkinter.TclError
- getElementsByName(query, _root=None)
Return all elements that match a given given name attribute.
- Parameters:
query (str) – The name to be searched for.
- Return type:
- Raises:
tkinter.TclError
- getElementsByTagName(query, _root=None)
Return all elements that match a given tag name.
- Parameters:
query (str) – The tag to be searched for.
- Return type:
- Raises:
tkinter.TclError
- querySelector(query, _root=None)
Return the first element that matches a given CSS selector.
- Parameters:
query (str) – The CSS selector to be searched for.
- Return type:
- Raises:
tkinter.TclError
- class tkinterweb.dom.HTMLElement(document_manager, node)
- Parameters:
document_manager (
HTMLDocument) – TheHTMLDocumentinstance this class is tied to.node (Tkhtml3 node) – The Tkhtml3 node this class represents.
- Variables:
document – The element’s corresponding
HTMLDocumentinstance.html – The element’s corresponding
TkinterWebinstance.node – The element’s corresponding Tkhtml node.
- property style
Manage the element’s styling. For instance, to make the element have a blue background, use
yourhtmlelement.style.backgroundColor = "blue".- Return type:
- property innerHTML
Get and set the inner HTML of the element. Cannot be set on
<html>elements.- Return type:
- Raises:
tkinter.TclError
- property innerText
Get and set the text content of an element, as displayed. Cannot be set on
<html>elements.- Return type:
- Raises:
tkinter.TclError
New in version 4.14.
- property textContent
Get and set the text content of an element. Cannot be used on
<html>elements.- Return type:
- Raises:
tkinter.TclError
- property parentElement
Get the element’s parent element.
- Return type:
- Raises:
tkinter.TclError
- property previousSibling
Get the element’s preceding sibling.
- Return type:
New in version 4.8.
- property nextSibling
Get the element’s following sibling.
- Return type:
New in version 4.8.
- property widget
Get and set the element’s widget.
Prior to version 4.2 this only applies to
<object>elements and a widget must be specified.Since version 4.10 this can also be used to get the widget representing
<input>,<textarea>,<select>,<iframe>, and some<object>elements.If the widget already exists in the document, it will first be removed from its previous element.
Ensure your HtmlFrame widget was created before the widget you are embedding, or else the widget might not be visible.
- Return type:
tkinter.Widgetor None
New in version 4.1.
- property value
Get and set the input’s value. Only works on
<input>,<textarea>,<select>, andprogresselements.- Return type:
New in version 4.1.
- property checked
Convenience property for the
checkedHTML attribute. Check/uncheck a radiobutton or checkbox or see if the element is checked.- Return type:
New in version 4.1.
- getAttribute(attribute)
Return the value of the given attribute.
- setAttribute(attribute, value)
Set the value of the given attribute.
- removeAttribute(attribute)
Remove the given attribute. At the moment this sets the value of the attribute to “”.
- Parameters:
attribute (str) – The attribute to remove.
New in version 4.20.
- remove()
Delete the element. Cannot be used on
<html>or<body>elements. The element can be reinserted into the document later if needed.- Raises:
tkinter.TclError
- appendChild(children)
Insert the specified children into the element.
- Parameters:
children (list, tuple, or
HTMLElement) – The element(s) to be added into the element.
- insertBefore(children, before)
Insert the specified children before a given child element.
- Parameters:
children (list, tuple, or
HTMLElement) – The element(s) to be added into the element.before (
HTMLElement) – The child element that the added elements should be placed before.
- Raises:
tkinter.TclError
- getElementById(query)
Return an element that is a child of the current element and matches the given id.
- Parameters:
query (str) – The element id to be searched for.
- Return type:
- Raises:
tkinter.TclError
- getElementsByClassName(query)
Return all elements that are children of the current element and match the given class name.
- Parameters:
query (str) – The class to be searched for.
- Return type:
- Raises:
tkinter.TclError
- getElementsByName(query)
Return all elements that are children of the current element and match the given name attribute.
- Parameters:
query (str) – The name to be searched for.
- Return type:
- Raises:
tkinter.TclError
- getElementsByTagName(query)
Return all elements that are children of the current element and match the given tag name.
- Parameters:
query (str) – The tag to be searched for.
- Return type:
- Raises:
tkinter.TclError
- querySelector(query)
Return the first element that is a child of the current element and matches the given CSS selector.
- Parameters:
query (str) – The CSS selector to be searched for.
- Return type:
- Raises:
tkinter.TclError
- querySelectorAll(query)
Return all elements that are children of the current element and match the given CSS selector.
- Parameters:
query (str) – The CSS selector to be searched for.
- Return type:
- Raises:
tkinter.TclError
- scrollIntoView()
Scroll the viewport so that this element is visible.
- bind(sequence, func, add=None)
Tkinter-style method to add a binding to this element.
The following virtual events are supported:
<<ElementLoaded>>/utilities.ELEMENT_LOADED_EVENT: Generated whenever an element loads after the page finishes.<<Modified>>`/utilities.FIELD_CHANGED_EVENT: Generated whenever the content of any<input>element changes.
The following Tkinter events are supported:
<Enter><Leave><MouseWheel>All Tkinter button and motion events
- Parameters:
- Raises:
RuntimeError – If events are disabled.
New in version 4.10.
The following JavaScript event properties are also supported: onchange, onclick, oncontextmenu, ondblclick, onload, onmousedown, onmouseenter, onmouseleave, onmousemove, onmouseout, onmouseover, and onmouseup.
- class tkinterweb.dom.HTMLCollection(document_manager, search_string, root=None)
This class stores results from various
HTMLElementmethods. It behaves like a Python list, with some extras.- Parameters:
document_manager (
HTMLDocument) – TheHTMLDocumentinstance this class is tied to.search_string (str) – The CSS query string to search using.
root (Tkhtml3 node) – The Tkhtml node to search.
- Variables:
html – The element’s corresponding
TkinterWebinstance.node – The element’s corresponding Tkhtml node.
New in version 4.4.
- property length
Returns the number of items in the collection.
- item(index)
Returns the element at the given index into the list.
- Parameters:
index (int) – The index of the element to get.
- Return type:
- Raise:
KeyError
- namedItem(key)
Returns the element whose id or name matches key.
- Parameters:
key (str) – The id or name to search for.
- Return type:
HTMLElementor None
- class tkinterweb.dom.CSSStyleDeclaration(element_manager)
Access this class via the
styleproperty of theHTMLElementclass.- Parameters:
element_manager (
HTMLElement) – TheHTMLElementinstance this class is tied to.- Variables:
html – The element’s corresponding
TkinterWebinstance.node – The element’s corresponding Tkhtml node.
- property cssText
Get and set the element’s inline style declaration.
- Return type:
Updated in version 4.19.
- property length
Return the number of style declarations in the element’s inline style declaration.
- Return type:
- property cssInlineProperties
Return all inline properties for the element. Similar to the
cssTextproperty, but formatted as a dictionary.- Return type:
- property cssInlineStyles
Return the content of the element’s
styleattribute, formatted as a dictionary.- Return type:
- getPropertyPriority(property)
Return the priority of the given inline CSS property.
- getPropertyValue(property)
Return the value of the given inline CSS property.
You can also use JavaScript-style camel-cased properties to get a CSS property value. For instance, to get the element’s background color, use
yourhtmlelement.style.backgroundColorNew in version 4.1.
- removeProperty(property)
Remove the given inline CSS property.
- Parameters:
property (str) – The CSS property to remove.
- Returns:
The old value of the given property, or “” if the property did not exist.
- Return type:
New in version 4.1.
- setProperty(property, value)
Set the value of the given inline CSS property.
You can also use JavaScript-style camel-cased properties to set a CSS property value. For instance, to make the element have a blue background, use
yourhtmlelement.style.backgroundColor = "blue"- Parameters:
property (str) – The CSS property to set.
- Returns:
The old value of the given property, or “” if the property did not exist.
- Return type:
New in version 4.1.
- class tkinterweb.dom.DOMRect(element_manager)
This class generates and stores information about the element’s position and size at this point in time.
- Parameters:
element_manager (
HTMLElement) – TheHTMLElementinstance this class is tied to.- Variables:
x – The element’s horizontal offset from the left-hand side of the page.
y – The element’s vertical offset from the top of the page.
width – The element’s width.
height – The element’s height.
html – The element’s corresponding
TkinterWebinstance.node – The element’s corresponding Tkhtml node.
Special thanks to Zamy846692 for the help making this happen!