TkinterWeb

repository·main·Indexed 42 days ago

https://github.com/andereoo/tkinterweb

A lightweight library that adds HTML and CSS rendering capabilities to Tkinter widgets. It provides widgets like HtmlFrame for displaying websites, HtmlLabel for styled content, and HtmlText for rich-text editing. The library includes a Python implementation of the Document Object Model (DOM) for modifying document appearance and supports optional extensions for JavaScript execution via PythonMonkey and enhanced CSS via TkinterWeb-Tkhtml-Extras.

Tokens
11.4K
Snippets
33
Records
59
Agent score
53%

What's inside TkinterWeb

  1. Overview of TkinterWeb capabilities

    main

    TkinterWeb is a Python library that integrates HTML and CSS rendering capabilities directly into Tkinter widgets. It allows developers to move beyond standard Tkinter limitations by enabling modern UI elements and web-based content within desktop applications.

    Key Use Cases:

    • Displaying help files and documentation.
    • Rendering images, including SVG support.
    • Building rich-text editors.
    • Designing applications using HTML templates.
    • Creating modern interfaces with advanced styling (e.g., round buttons).
  2. Embed Python code in HTML documents

    main

    To execute embedded <script> blocks as Python code instead of JavaScript, configure your widget with both javascript_enabled=True and javascript_backend="python".

    By default, these scripts can access the document property, and inline event callbacks can access the this property. You must register new objects via .javascript.register if you want the scripts to access specific Python functions, classes, or variables.

    yourhtmlframe = tkinterweb.HtmlFrame(root, javascript_enabled=True, javascript_backend="python")
  3. Core TkinterWeb Widgets

    main

    TkinterWeb provides three primary widgets for different HTML rendering and editing needs:

    1. HtmlFrame: A frame widget used to display and edit websites, help files, RSS feeds, and other styled HTML.
    2. HtmlLabel: A label widget designed to display styled HTML content.
    3. HtmlText: A text widget that allows users to edit styled HTML content.
  4. Interact with the Document Object Model (DOM) in tkinterweb

    main

    The tkinterweb.dom module provides a Python implementation of the Document Object Model (DOM), allowing you to modify the appearance and content of a loaded document and manage user interactions. The API is designed to mirror the core JavaScript DOM API, making it familiar to web developers.

    Key classes available in this module include:

    • HTMLDocument: Represents the entire loaded document.
    • HTMLElement: Represents individual elements within the document.
    • HTMLCollection: A collection of elements (e.g., returned by getElementsByTagName).
    • CSSStyleDeclaration: Represents the inline styles of an element.
    • DOMRect: Represents the geometry of an element.
  5. Identify the core TkinterWeb widgets

    main

    TkinterWeb provides three primary widget types for integrating web content into your Tkinter applications:

    1. HtmlFrame: A frame widget designed for full web page rendering.
    2. HtmlLabel: A label widget for displaying HTML content within a label structure.
    3. HtmlText: A text widget for rendering HTML within a text-based area.

    For detailed implementation details, refer to the API documentation.

  6. Use tkinterweb.Notebook instead of ttk.Notebook

    main

    When building applications that use tkinterweb (specifically those using Tkhtml), you should use tkinterweb.Notebook instead of the standard ttk.Notebook. The standard ttk.Notebook is incompatible with Tkhtml on 64-bit Windows and can cause application crashes when selecting tabs.

    tkinterweb.Notebook implements the same API as ttk.Notebook as defined in the standard Python documentation.

  7. Use the HtmlFrame widget for embedded web content

    main
    The tkinterweb.HtmlFrame widget is an enhanced Tkinter frame that wraps the TkinterWeb widget. It provides a more complete out-of-the-box experience by including automatic scrollbars, error handling, and various convenience methods. It is designed to be easily embeddable and can also be used to manage other Tkinter widgets, allowing for easy integration between standard Tkinter UI elements and HTML content.
  8. Avoid crashes when using Notebooks on 64-bit Windows

    main

    On 64-bit Windows, the underlying HTML engine (Tkhtml) is incompatible with the standard ttk.Notebook widget, which causes application crashes.

    Workaround: Use tkinterweb.Notebook instead of ttk.Notebook. It is a drop-in replacement designed to behave like ttk.Notebook without the crashes.

    Important Note on Layout: When using tkinterweb.Notebook, do not call .pack() or .grid() on a widget after adding it to the notebook (e.g., after mynotebook.add(mywidget)). The tkinterweb.Notebook handles the layout automatically, and manual calls to pack/grid may raise errors.

    from tkinterweb import Notebook
    import tkinter as tk
    
    root = tk.Tk()
    # Use tkinterweb.Notebook instead of ttk.Notebook
    notebook = Notebook(root)
    notebook.pack()
    
    # Create a frame
    my_frame = HtmlFrame(notebook)
    
    # Add to notebook - DO NOT call my_frame.pack() or my_frame.grid() here
    notebook.add(my_frame)
  9. How to use Shrink to make HTML widgets behave like labels

    main

    The shrink feature allows HTML widgets to automatically resize to match their content, mimicking the behavior of a standard ttk.Label.

    You can achieve this in two ways:

    1. Use the .HtmlLabel widget for a widget that looks and behaves like a ttk.Label.
    2. Add the parameter shrink=True to an .HtmlFrame widget if you require more control.

    Note: This API changed in version 4.17.

    # Option 1: Using HtmlLabel
    label = HtmlLabel(root, text="Hello World")
    
    # Option 2: Using HtmlFrame with shrink enabled
    frame = HtmlFrame(root, shrink=True)
  10. Understand TkinterWeb Extensions

    main

    TkinterWeb provides several extension objects that augment the core tkinterweb.TkinterWeb widget. These extensions handle specific internal responsibilities such as text selection, caret (cursor) management, event handling, widget management, and search functionality.

    Warning: These extensions are considered largely internal. Their APIs are not stable and may change at any time without warning. Use them only if necessary for advanced customization and be aware that updates to tkinterweb might break your implementation.

  11. Quickstart: Load HTML with HtmlFrame

    main

    The HtmlFrame widget is the primary way to display web content. You can load raw HTML strings, local files, or URLs.

    To load raw HTML, use load_html(). To load websites or files, use load_website(), load_file(), or load_url().

    import tkinter as tk
    from tkinterweb import HtmlFrame
    
    root = tk.Tk()
    
    # Create the HtmlFrame widget
    yourhtmlframe = HtmlFrame(root, messages_enabled=True)
    
    # Load HTML code
    yourhtmlframe.load_html("<h1>Hello, World!</h1>")
    
    yourhtmlframe.pack(fill="both", expand=True)
    
    root.mainloop()
  12. Quickstart: Display a website with HtmlFrame

    main

    To display and interact with a website in a Tkinter application, use the HtmlFrame widget. This widget allows you to load URLs and render HTML content within your Tkinter window.

    import tkinter as tk
    from tkinterweb import HtmlFrame # import the HtmlFrame widget
    
    root = tk.Tk() # create the Tkinter window
    frame = HtmlFrame(root, messages_enabled=True) # create the HtmlFrame widget
    frame.load_website("https://tkinterweb.readthedocs.io/en/latest/index.html") # load a website
    frame.pack(fill="both", expand=True) # attach the HtmlFrame widget to the window
    root.mainloop()