第一种方式
第二种方式
完整代码
前端代码
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>pywebview example</title>
</head><body><h1>hello pywebview!</h1><p>this is a simple desktop application using pywebview</p><button onclick="GetMessageFromBackground()">Get Message</button><p id="message"></p><script>/*async function GetMessageFromBackground() {const message = await window.pywebview.api.getMessage();document.getElementById("message").innerText = message}*/function GetMessageFromBackground() {window.pywebview.api.getMessage().then(response => {document.getElementById("message").innerText = response});}</script>
</body></html>
后端代码
import webview
import os
import jsonclass API:def getMessage(self):return 'hello from python background'
def main():api=API()webview.create_window('pywebview example','index.html',js_api=api)webview.start()
if __name__=='__main__':main()