javascript - 如何在python eel中的多个javascript文件之间进行通信
问题描述
我正在尝试使用 eel 制作 HTML 应用程序。在这个应用程序中,我有两个 javascript 文件,并希望在我的 python main.py 文件之间进行通信。它将从“show()”和“show2()”中获取一个字符串。“show()”中的字符串将进入我的“script.js”文件,“show2()”中的字符串将进入“script2” .js”。我可以从“show()”中获取字符串,但不能从 show2() 中获取。只有一个 javascript 文件在工作,另一个不工作。有人可以帮我吗?
这是我的python“main.py”代码:
import eel
import time
import wikipedia
import socket
import os
import pyttsx3
import webbrowser
import datetime
import sound
engine= pyttsx3.init("sapi5")
en_id = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Speech\\Voices\\Tokens\\TTS_MS_Cortana"
engine.setProperty("voice",en_id)
def say(audio):
engine.say(audio)
engine.runAndWait()
@eel.expose
def show(text):
user=text.lower()
if "hello" in user or "hey" in user:
ans="Hello, how can I help you?"
time.sleep(0.5)
eel.boxes2(ans)
say(ans)
@eel.expose
def show2(text2):
print(text2)
eel.boxes3("hello")
eel.init("eel")
eel.start("index.html",size=(460,660))
这是我的“script.js”文件。该文件正在运行:
var nav=document.getElementById("content");
var textbox=document.getElementById("input");
var tag=document.createElement("i");
textbox.onclick=function(){
nav.style.display="none";
}
eel.expose(boxes2);
function boxes2(reply){
var tag=document.createElement("a");
var text=document.createTextNode(reply);
tag.appendChild(text);
var element=document.getElementById("box2");
element.appendChild(tag);
}
function boxes(){
var textbox=document.getElementById("input");
text= textbox.value;
eel.show(text)
var cover=document.createElement("p");
var tag=document.createElement("i");
var text=document.createTextNode(text);
tag.appendChild(text);
cover.appendChild(tag);
var element=document.getElementById("box2");
element.appendChild(cover);
textbox.value="";
}
var sub = document.getElementById("send");
sub.onclick=function(){
boxes();
boxes2(reply);
}
这是我的“script2.js”。这是在 python eel 中不起作用的文件:
eel.expose(boxes3)
function boxes3(reply2){
eel.show2("bye")
var tag=document.createElement("p");
var text=document.createTextNode(reply2);
tag.appendChild(text);
var element=document.getElementById("box2");
element.appendChild(tag);
}
var sub2 = document.getElementById("hello");
sub2.onclick=function(){
boxes3(reply2);
}
这是我的 index.html 代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no">
<title>Aurora</title>
<link rel="Icon" href="logo.ico" type="png" hreflang="en">
<link rel="stylesheet" href="style.css">
</head>
<body>
<div style="background-color: blanchedalmond; width: 500px; height: 30px; position: fixed; top: 0;">
<label id="options" title="options">...</label>
<button id="hello" >hello</button>
</div>
<div style="width: max-content; height: 630px; overflow-y: scroll; overflow-x: hidden;scroll-behavior: auto;">
<div id="box2" style="width: 470px; height: max-content; margin-top: 35pt;margin-left: 20px;"></div>
</div>
<div id="content">
<label id="close" title="close">×</label>
<h1 style="font-family: segoe ui; position: relative; top: -50px;left: 10px; font-weight: 300;">Aurora</h1>
<button style="width: 100%; font-size: 15pt; text-align: left; position: relative; top: 220pt; border:none; padding: 7pt;outline: none; " id="settings">Settings</button>
<button style="width: 100%; font-size: 15pt; text-align: left; position: relative; top: 220pt; border: none; padding: 7pt;outline: none; " id="settings">About</button>
<button style="width: 100%; font-size: 15pt; text-align: left; position: relative; top: 220pt; border: none; padding: 7pt;outline: none; " id="settings">Commands</button>
</div>
<div id="user-input">
<form>
<textarea id="input" placeholder="Ask me something" title="ask aurora"></textarea>
</form>
<button id="send" title="send text">Send</button>
</div>
<script src="eel.js"></script>
<script src="script.js"></script>
<script src="script2.js"></script>
</body>
</html>
请帮我!!!
解决方案
所有用@eel.expose 修饰的函数都必须在eel.init 和eel.start 之间。
尝试使用以下行:
eel.init('eel', allowed_extensions=['.js', '.html'])
这将在您的 eel 文件夹中包含的所有 js 和 html 文件中查找公开的函数。
推荐阅读
- python - 在 Makefile 中使用环境变量作为 python 参数失败
- javascript - 如何在不声明的情况下使用新数组扩展数组
- linux - 用 pwd 和 awk 理解 shell 中的美元
- angular - Angular Subcribe - 类型 'Subscription' 缺少来自类型 'ToDo[]' 的以下属性:length、pop、push、concat 和另外 26 个
- html - 为移动设备调整页脚大小
- url-parameters - 如何在谷歌数据工作室中使用单个 url 参数进行多个查询?
- chronicle - 失败后如何恢复写入队列
- android - 如何从 Kotlin 的数据类中更改特定变量?
- javascript - 如何将脚本存储在字符串中?
- node.js - AWS Lambda 节点 - 中断后重新启动