Skip to main content

tkwebview

tkinter的webview视图。

简介

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

显然,直接使用c/c++封装给python使用会更简单。感谢webview/webviewHIllya51/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布局。

如果masterNone,将独立打开一个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中编写,本库只提供统一封装。

Download files

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

Source Distribution

tkwebview-0.3.0.tar.gz (106.7 kB view details)

Uploaded Source

Built Distribution

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

tkwebview-0.3.0-py3-none-any.whl (104.1 kB view details)

Uploaded Python 3

File details

Details for the file tkwebview-0.3.0.tar.gz.

File metadata

  • Download URL: tkwebview-0.3.0.tar.gz
  • Upload date:
  • Size: 106.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for tkwebview-0.3.0.tar.gz
Algorithm Hash digest
SHA256 5116adb9f168f750089308eaa4f7debb1292c258e74f9059d903b62e7fdaa477
MD5 8a2f0b3a46e217c9a03b30bd8de29ed1
BLAKE2b-256 3cbdf63afeeeed5f14d1582d5342cc827b3b301bb8de9c49cd7a39e2be5b223a

See more details on using hashes here.

File details

Details for the file tkwebview-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: tkwebview-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 104.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for tkwebview-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 150bf4f5ca5a51e3557581e79d0ceb423dfa739f95760eaf1298d78b418c66cb
MD5 01a978776265d717149cab9cc05acc68
BLAKE2b-256 f9dfac5c1ee029c00b1063a4daedbdd8196a06f342e433f0b21ed4739eff7be7

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 files

0.2.0

2 files

0.1.1

2 files

0.1.0

2 files

Supported by

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