另辟蹊径:python利用pywebview用HTML技术实现GUI

pywebview

官网:https://pywebview.flowrl.com/

Build GUI for your Python program with JavaScript, HTML, and CSS

最简单的实例

实现了python与html的双向交互

  • python文件
import webview
import random


def test():
    return random.randint(1, 1000)


def expose(window):
    window.evaluate_js('alert("hello from python!")')  # python里执行js


if __name__ == '__main__':
    win = webview.create_window('JS Expose Example', 'index.html', text_select=True)
    win.expose(test)  # 允许html页面里访问test
    webview.start(expose, win)  # 启动webview时调用expose
  • HTML文件:index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1 onclick="pywebview.api.test().then(function(res){alert(res)})">click me to call python function!</h1>
</body>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容