如何使用 pywebview api 从 javascript 关闭 pywebview 窗口

Zah*_*ale 1 html javascript flask python-3.x pywebview

我正在使用 html 和 Flask 在 pywebview 中构建一个应用程序,我决定使用全屏模式并制作自定义的漂亮按钮以最小化和退出 ie( window.open('','_self').close();) 。经过一些研究后,我们用于退出或最小化 Firefox 中的 winndow 的正常 JavaScript 代码不起作用我得到了这个https://pywebview.flowrl.com/examples/js_api.html但我不知道如何使用如果有人可以编写并解释它,则此 api 可以退出应用程序,因为如果只是有人发布代码,我不明白它

这是我的应用程序代码

from flask import Flask
from flask import render_template, jsonify, request
import webview
import sys
import threading
import http.server
import socketserver
from os import system
system("title Fire - By mzw")
state=1
app=Flask(__name__)
app.config["CACHE_TYPE"] = "null"
@app.route('/')
def home():
    return render_template('start.html', name="Fire")
def start_server():
    cli = sys.modules['flask.cli']
    cli.show_server_banner = lambda *x: None
    app.run(host='0.0.0.0',port=5000)
def start_scanner():
    PORT = 5050
    Handler = http.server.SimpleHTTPRequestHandler
    with socketserver.TCPServer(("", PORT), Handler) as httpd:
        httpd.serve_forever()
if __name__=='__main__':
    t=threading.Thread(target=start_server)
    t.daemon=True
    t.start()
    b=threading.Thread(target=start_scanner)
    b.daemon=True
    b.start()
    webview.create_window("Fire","http://localhost:5000/",fullscreen=True)
    webview.start()
    sys.exit()
Run Code Online (Sandbox Code Playgroud)

这是我的 html 代码

<body>
    <nav class="navbar navbar-expand-lg navbar-light bg-amber">
        <a class="navbar-brand" href="#">{{name}}</a>
        <button class="navbar-toggler bg-amber" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon bg-amber"></span>
        </button>
        <div class="collapse navbar-collapse bg-amber" id="navbarNavAltMarkup">
            <div class="navbar-nav bg-amber">
                <a class="nav-item nav-link active" href="#">Home</a>
                <a class="nav-item nav-link" href="#">Features</a>
                <a class="nav-item nav-link" href="#">Pricing</a>
                <a class="nav-item nav-link disabled" href="#">Disabled</a>
            </div>
        </div>
        <div class="float-right bg-amber">
            <a href="#"><img style="width: 30px;height: 30px;" src="{{ url_for('static', filename='green.png') }}"></a>
            <a href="#" id="minimize"><img style="width: 30px;height: 30px;" src="{{ url_for('static', filename='blue.png') }}"></a>
            <a href="#"><img style="width: 30px;height: 30px;" src="{{ url_for('static', filename='red.png') }}"></a>
        </div>
        <script type="text/javascript">
            $("#minimize").click(function(){
                 window.open('','_self').close();
            });
        </script>
    </nav>
</body>
Run Code Online (Sandbox Code Playgroud)

vma*_*san 5

您只需要添加一个Api在 Python 代码中调用的类,就像您链接的文章中所解释的那样。在您的情况下,该类只需要包含构造函数和如下所示的方法:

def quit(self):
    self._window.destroy()
Run Code Online (Sandbox Code Playgroud)

(以及您需要从 python 执行的任何其他方法),然后使用pywebview.api.quit().

更新:忘记提及一个重要细节。创建窗口时,将其存储在变量中,如下所示:

  api = Api()

  window = webview.create_window(
    ...
    js_api=api,
    ...
  )
  api.set_window(window)
Run Code Online (Sandbox Code Playgroud)

所以你的 Api 类看起来像这样:

class Api:

  def __init__(self):
    self._window = None

  def set_window(self, window):
    self._window = window

  def quit(self):
    self._window.destroy()
Run Code Online (Sandbox Code Playgroud)