Skip to main content

tkwebview

tkinter的webview视图。

简介

在此之前,我在Windows平台上实现了Smart-Space/tkwebview2,但是为了使用WebView2,需要pywebview和pythonnet,而且有大量的底层操作包含在库源码中。

显然,直接使用c/c++封装给python使用会更简单。感谢webview/webview和HIllya51/webviewpy,让这个想法成为可能。

[!IMPORTANT]

tkwebview当前不包含事件回调,但是拥有了十分重要的javascript调用python代码的功能。此外,因为经过封装,tkwebview难以操作原始的web控件,但也因此非常简单易用。

[!WARNING]

虽然webview是跨平台的,但是原始的set_size无法满足嵌入状态下的尺寸修改,需要实现新的函数,并且为了方便,我单独维护了一个基于webview的c++库Smart-Space/webview,这个库的核心部件截止于HIllya51/webviewpy@e81bd17,如果没有严重错误,不会更新。

由于条件限制,本仓库只能提供的Windows平台的二进制链接库(32位链接库未经过测试)。

开始使用

pip install tkwebview

示例程序见:../tkwebview/test.py

主要方法/函数

TkWebview(master, **kwargs)

tkwebview的核心类,本质上是一个Frame,可以和Frame布局。

如果master为None,将独立打开一个webview窗口,使用类似webviewpy,但如果要独立运行,需要使用tkwebview.webview.run()开启UI循环。

tkwebview.bindjs(name, fn, is_async_return=False)

绑定名为name的JS函数,指向fn。在JavaScript中,使用window.name调用。

在同步模式中,fn只需要接收JS函数传递来的参数,返回结果即可。

异步模式中,fn至少会收到一个returner函数(第一个参数位),异步处理得到结果后,通过returner(result)返回结果。

tkwebview.dispatch(fn)

子线程中调度代码在主线程/GUI上运行(我也没看懂)。

tkwebview.unbindjs(name)

解除绑定名为name的JS函数。

tkwebview.eval(js)

执行JS代码。

tkwebview.navigate(url)

导航到url。

[!NOTE]

url需要以协议开头,包括http, https, file...

tkwebview.init(js)

在开头注入JS代码,比任何内容都要前。

tkwebview.set_html(html)

设置html代码。

tkwebview.version(html)

返回webview/webview的版本。

tkwebview.reload()

Windows Only

重新加载。

tkwebview.go_back()

Windows Only

向后一页。

tkwebview.go_forward()

Windows Only

向前一页。

tkwebview.stop()

Windows Only

停止加载。

tkwebview.set_title(title)

独立模式

设置窗口标题。

tkwebview.destroy_webview()

独立模式

关闭webview窗口。

控件模式

销毁本控件

tkwebview.get_window()

独立模式

返回窗口ID。

  • Windows:HWND
  • Linux:GtkWindow指针
  • Cocoa:NSWindow指针

控件模式

返回上层组件ID。

  • Windows:HWND
  • Linux:X11窗口标识

tkwebview.set_size(width, height)

独立模式

设置窗口大小。

控件模式

尝试修改控件大小。

[!CAUTION]

Windows平台下,不知原因,tkinter界面中,WebView2具有最高级渲染优先级,一旦获得焦点,除非离开整个窗口,否则其它控件无法获取焦点。解决方法是为所有控件绑定<Button-1>使用event.widget.focus_force()强制获取焦点。如果有控件还需要额外绑定<Button-1>事件,使用add参数。

开发

第一步

下载Smart-Space/webview,编译需要的版本,得到动态链接库。

我只实现了Windows平台下的webview_resize。

第二布

下载本仓库代码,将动态链接库放置在.tkwebview/platform下正确的位置(见./tkwebview/core.py)。

关于跨平台

理论上,tkwebview完全能够跨平台,但是我测试不了,所以只提供Windows平台的二进制链接库。所有的跨平台代码应当在webview中编写,本库只提供统一封装。

Metadata

Release files for tkwebview 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tkwebview 0.3.0
File Size Uploaded
tkwebview-0.3.0.tar.gz 106.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tkwebview 0.3.0
File Interpreter ABI Platform
tkwebview-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 210.8 kB

Release files / tkwebview-0.3.0.tar.gz

Download URL tkwebview-0.3.0.tar.gz
Size 106.7 kB
Tags Source
SHA-256 checksum
How to use checksums
5116adb9f168f750089308eaa4f7debb1292c258e74f9059d903b62e7fdaa477
BLAKE2b-256 checksum
How to use checksums
3cbdf63afeeeed5f14d1582d5342cc827b3b301bb8de9c49cd7a39e2be5b223a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Release files / tkwebview-0.3.0-py3-none-any.whl

Download URL tkwebview-0.3.0-py3-none-any.whl
Size 104.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
150bf4f5ca5a51e3557581e79d0ceb423dfa739f95760eaf1298d78b418c66cb
BLAKE2b-256 checksum
How to use checksums
f9dfac5c1ee029c00b1063a4daedbdd8196a06f342e433f0b21ed4739eff7be7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page