Skip to main content

Keep Tkinter — embed a real system WebView in your Frame (wry, child-window embedding)

Project description

tkwry

License: MIT PyPI - Python Version GitHub Release PyPI Version Downloads Status: Alpha CI

Keep Tkinter — give it the WebView it never had.

Embed a real system WebView (wry) inside your Frame: modern HTML, JS, and IPC in the same layout as your buttons and tabs — one mainloop, no floating overlay.

Alpha — Early preview (see PyPI badge for the current version). APIs and behavior may change without notice. Not recommended for production use yet.


📖 Overview

Tkinter is still a solid GUI shell — it just had no first-class way to host modern web content inside a widget. Overlay-style WebViews drift out of sync when you move, resize, or switch tabs.

tkwry fills that missing piece:

  • True child embeddingbuild_as_child via HWND, NSView, or X11 window ID
  • One event loop — Tk mainloop only; no separate app runtime
  • IPC bridge — JavaScript → Python callbacks without freezing the UI
  • Layout-aware — tracks pack / grid / place, tabs, and PanedWindow

Pre-built abi3 wheels ship for Windows (x86_64) and macOS (Apple Silicon + Intel) — these are the primary release targets. Linux is best-effort: build from source (sdist / git); timing and headless behavior are not guaranteed in v0.0.x.


🔧 Requirements

  • Python 3.10+
  • Tkinter (included with most Python builds)
  • Windows (x86_64 only)
    • WebView2 Runtime must be installed (pre-installed on many Windows 10/11 systems).
    • Without WebView2, tkwry is not supported on Windows — there is no fallback engine.
  • macOS — 11 (Big Sur) or later; Apple Silicon (arm64) or Intel (x86_64); system WKWebView (no extra runtime)
  • Linux — WebKitGTK 4.1 + GTK 3 dev packages; X11 or XWayland ($DISPLAY); build the extension from source (see below)

📦 Installation

Install from PyPI (Windows / macOS wheels):

pip install tkwry

Developing or running examples from a git clone — use an editable install so Python picks up this repo (not an older copy in site-packages):

pip install -e .

Or install from GitHub for the latest changes:

pip install git+https://github.com/mashu3/tkwry.git

Linux (source install, best-effort)

Linux builds from source and runs for many apps, but v0.0.x does not treat Linux stability as a release requirement — focus is on Windows and macOS wheels. Install system dependencies, then:

# Debian / Ubuntu
sudo apt install \
  libwebkit2gtk-4.1-dev \
  libgtk-3-dev \
  libglib2.0-dev

# Runtime (for end users of your app)
# sudo apt install libwebkit2gtk-4.1-0 libgtk-3-0

pip install maturin
git clone https://github.com/mashu3/tkwry.git
cd tkwry
pip install .

GTK events are pumped automatically on a Tk timer while your app runs.


🚀 Usage

Basic WebView

import tkinter as tk
from tkwry import WebView

root = tk.Tk()
root.geometry("900x600")

frame = tk.Frame(root, bg="#222")
frame.pack(fill="both", expand=True, padx=8, pady=8)

web = WebView(frame, url="https://github.com")

root.mainloop()

IPC (JavaScript → Python)

def on_message(msg: str) -> None:
    print("from JS:", msg)

web = WebView(
    frame,
    html='<button onclick="window.ipc.postMessage(\'hi\')">send</button>',
    ipc_handler=on_message,
)

Load HTML / evaluate JavaScript

web.load_html("<h1>Hello</h1>")
web.eval_js("document.title = 'Hi'")  # fire-and-forget (Tk idle, no return value)
web.eval_js_with_callback("document.title", print)  # async; callback on Tk main thread
web.load_url("https://example.com")
web.reload()
print(web.url)
web.focus()
web.open_devtools()

Rapid load_url / load_html calls are coalesced (last-wins)load(A); load(B); load(C) loads C only.

eval_js does not return a result (not synchronous). Use eval_js_with_callback when you need the JavaScript return value as a str.

Layout / resize

Bounds sync runs automatically on <Configure>, <Map>, and <Unmap>. Call sync_bounds() manually after custom layout changes so the WebView reflows (e.g. centered images):

web.sync_bounds()

Navigation / lifecycle callbacks

from tkwry import NewWindowResponse, PageLoadEvent

web = WebView(
    frame,
    url="https://example.com",
    on_page_load=lambda evt, url: print(evt, url),
    on_title_changed=lambda title: root.title(title),
    on_navigation=lambda url: url.startswith("https://"),
    on_new_window=lambda url: NewWindowResponse.Deny,
)

on_page_load fires PageLoadEvent.Started and PageLoadEvent.Finished for every navigation. Events that occurred before a handler was registered are discarded when you call set_on_page_load (or pass on_page_load in the constructor from the start).

Callback exceptions are printed to stderr and do not stop event delivery.

Drag & drop (native OS path)

File drops from Finder / Explorer are handled by the OS WebView. Your handler runs on the Tk main thread (tkwry queues events from WebKit automatically).

from tkwry import DragDropEvent

def on_drop(event, paths, position):
    if event == DragDropEvent.Drop:
        print("files:", paths)

web = WebView(frame, html="...", drag_drop_handler=on_drop)

See examples/dnd_demo.py.

Cleanup

web.destroy()   # release native webview; host Frame is kept
# or destroy the host Frame — both tear down the webview

📚 API summary

Category Members
Content load_url, load_html, reload, url
JavaScript eval_js, eval_js_with_callback
IPC set_ipc_handler
Callbacks set_on_navigation, set_on_page_load, set_on_title_changed, set_on_new_window, set_drag_drop_handler
Appearance set_background_color, focus, focus_parent, open_devtools, close_devtools, is_devtools_open
Layout pack, grid, place, sync_bounds (delegate to host Frame except sync_bounds)
Lifecycle destroy, destroyed, native

Constructor options: url, html, ipc_handler, devtools, background_color, user_agent, initialization_script, focused, plus the callback hooks above.

Enums: PageLoadEvent, NewWindowResponse, DragDropEvent.


⚠️ Known limitations

  • Alpha — APIs may change; not recommended for production yet
  • Windows — WebView2 Runtime required; systems without it are unsupported
  • Linux — source install only (no PyPI wheel); best-effort in v0.0.x — headless CI and event timing are not release blockers
  • DevTools — macOS uses private APIs; avoid in Mac App Store release builds
  • macOS input — keyboard focus is shared between Tk and the WebView on the main thread; typing may feel slightly less snappy than a standalone browser (especially with IME on)
  • Drag & drop — drop target is the WebView area only (not arbitrary Tk widgets; use tkinterdnd2 for those)

See CHANGELOG.md for release history.


🌐 Platform notes

OS Arch Parent handle Engine Notes
Windows x86_64 Frame.winfo_id() → HWND WebView2 WebView2 Runtime required
macOS arm64, x86_64 Toplevel content NSView WKWebView See macOS embedding below
Linux winfo_id() → X11 window ID WebKitGTK Source install; best-effort (not a wheel release target)

macOS embedding

On macOS, Tk child Frames usually do not get their own NSView — that is a property of the Tk Aqua backend, not something tkwry can turn into per-frame native views without upstream Tk changes.

tkwry works around this by:

  1. Attaching the WebView to the toplevel content view
  2. Positioning it with set_bounds to match your Frame (<Configure>)
  3. Hiding it with set_visible(False) when the frame is unmapped (<Unmap>) — e.g. another ttk.Notebook tab is selected

Keyboard focus (macOS): Rust routes clicks between Tk widgets and the WebView. You may notice slight input latency compared to a normal browser; v0.0.1 treats this as a known limitation.

You do not need extra code for tabs or panes — see examples/multi_demo.py. IPC, page-load, title, eval, and drag-and-drop handlers are dispatched on the Tk main thread via an internal queue (avoids WebKit deadlocks).


💡 Why child-window embedding?

Tkinter apps already have a window and a layout. The web belongs inside a Frame — same mainloop, same tabs and panes — not in a separate top-level webview that floats beside your UI. tkwry wraps wry's build_as_child against the native surface Tk gives your widgets.


🧩 Features

  • Child-window embedding — WebView is a native child of your Tk window surface, not a floating overlay
  • Bounds & visibility sync — follows <Configure>, <Map>, and <Unmap> (tabs / Notebook work out of the box on macOS)
  • Deferred callbacks — IPC, page load, title, eval results, and DnD queue to Tk (avoids macOS deadlocks)
  • URL safety — normalizes and validates URLs before navigation
  • DevToolsopen_devtools() / devtools=True for debugging
  • Native drag & drop — OS-level file drops into the WebView (no tkinterdnd2)
  • Navigation hookson_navigation, on_page_load, on_title_changed, on_new_window
  • Multiple layouts — works with pack, grid, place, Notebook, and PanedWindow (see examples)
  • Plotly-ready — load HTML + eval_js for interactive charts
  • Alpha, but tested — CI on Windows and macOS; Linux CI is smoke/build only (best-effort)

📁 Examples

python -m venv .venv
source .venv/bin/activate   # Windows: .venv\Scripts\activate
pip install -e .
Script Description
examples/url_demo.py URL bar + embedded page
examples/ipc_demo.py JavaScript ↔ Tkinter IPC
examples/multi_demo.py Multiple WebViews, tabs, panes
examples/plotly_demo.py Plotly charts (pip install plotly)
examples/dnd_demo.py Native file drag & drop into WebView
python examples/url_demo.py
python examples/ipc_demo.py
python examples/multi_demo.py
python examples/plotly_demo.py
python examples/dnd_demo.py

📝 License

This project is licensed under the MIT License. See LICENSE.

This project links against wry, which is dual-licensed (Apache-2.0 or MIT). tkwry uses wry under MIT; see NOTICE for attribution.


👨‍💻 Author

mashu3

Contributors

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tkwry-0.0.3.tar.gz (64.2 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

tkwry-0.0.3-cp310-abi3-win_amd64.whl (343.0 kB view details)

Uploaded CPython 3.10+Windows x86-64

tkwry-0.0.3-cp310-abi3-macosx_11_0_arm64.whl (382.2 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

tkwry-0.0.3-cp310-abi3-macosx_10_12_x86_64.whl (405.7 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

Details for the file tkwry-0.0.3.tar.gz.

File metadata

  • Download URL: tkwry-0.0.3.tar.gz
  • Upload date:
  • Size: 64.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.14.1

File hashes

Hashes for tkwry-0.0.3.tar.gz
Algorithm Hash digest
SHA256 7e4d81a94bfed00aedf908093ecc06eeb8f4b984abda3c7650d24d4c5a8b3be1
MD5 dd5f0fc4afb37cb0c6ee3ebc2cba0638
BLAKE2b-256 c3dd282599c676c38277b45f73613151f5b49a9051d2c911a938073b4ee15f69

See more details on using hashes here.

File details

Details for the file tkwry-0.0.3-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: tkwry-0.0.3-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 343.0 kB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.14.1

File hashes

Hashes for tkwry-0.0.3-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 3e50daa5b697f2c47afaad468ed626204cce64ef054d48e84ffce2005b8da746
MD5 6e50084234b60f087b5a99a3c5e5086a
BLAKE2b-256 ed359e7662f4d33ea518487a4e2e6b08c2a4c80647c03ed88a35469464220900

See more details on using hashes here.

File details

Details for the file tkwry-0.0.3-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tkwry-0.0.3-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f5ed298ed67cff4efca946df2147c9d24b6c7466644c725b8f6ea5c38d82915f
MD5 c6bcb6e9919223a1b1ac464b606841b0
BLAKE2b-256 e616075cb22c10be879fa9bbcfec6f2e5eab5b8543b9148baf604eb9eba4e4ab

See more details on using hashes here.

File details

Details for the file tkwry-0.0.3-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for tkwry-0.0.3-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 9788ce4d333bae49477e47d249c9669574e7447de4e6a0a287312c46f6e6f681
MD5 8563acf1b1a3e87007960972c2e849c3
BLAKE2b-256 9a51dd440d3f700610115a7c3c0a8ace680e9ee877e3c45be2d624ac519f6a25

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page