我正在用html和烧瓶在pywebview中构建一个应用程序,我决定使用全屏幕模式,并为最小化和退出定制好看的按钮,即(window.open('','_self').close();)。我们在firefox中用于退出或最小化窗口的普通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()这是我的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>发布于 2021-02-09 22:04:48
您只需要在Python代码中添加一个名为Api的类,就像您链接的文章中解释的那样。在您的示例中,该类只需要包含构造函数和如下方法:
def quit(self):
self._window.destroy()(以及您需要从python执行的任何其他方法),然后使用pywebview.api.quit()从javascript调用它。
更新:忘记提到一个重要的细节。创建窗口时,将其存储在变量中,如下所示:
api = Api()
window = webview.create_window(
...
js_api=api,
...
)
api.set_window(window)所以你的Api课程应该是这样的:
class Api:
def __init__(self):
self._window = None
def set_window(self, window):
self._window = window
def quit(self):
self._window.destroy()https://stackoverflow.com/questions/65279193
复制相似问题