我正在尝试使用eel制作一个HTML应用程序。在此应用程序中,我有两个javascript文件,并希望在我的python main.py文件之间进行通信。它将从"show()“和" show2()”中获取一个字符串。“.The()”中的字符串将转到我的"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>
请帮帮我!
发布于 2021-01-03 15:25:38
所有用@eel.expose修饰的函数必须在eel.init和eel.start之间。
尝试使用下面这行代码:
eel.init('eel', allowed_extensions=['.js', '.html'])
这将在您的eel文件夹中包含的所有js和html文件中查找公开的函数。
https://stackoverflow.com/questions/65540240
复制相似问题