错误代码
正确示例
完整代码
前端代码
<!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>
function GetMessageFromBackground() {
window.pywebview.api.getMessage().then(response => {
document.getElementById("message").innerText = response
});
}
</script>
</body>
</html>
后端代码
import webview
import os
import json
class 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()