我正在嘗試學(xué)習(xí)使用 Python eel 庫(kù)。具體來(lái)說(shuō),我試圖通過(guò)單擊按鈕(運(yùn)行 JavaScript 函數(shù))來(lái)調(diào)用 Python 函數(shù)。在我的文件夾中有四個(gè)文件,排列如下:|+--- main.py|+--- web-gui | +--- index.html | +--- main.js | +--- style.css這是我的main.py:# Start GUI using eelimport eeleel.init('web-gui')eel.start('index.html', size=(1440, 900), block=False)# Boolean data from javascript (true or false) is given to pythonresult = eel.js_startPython()()print('Javascript says hello! Your result is', bool(result))這是我的main.js:// Javascript passes data to Python when button is pusheddocument.getElementById("data").addEventListener("click", js_startPython);eel.expose(js_startPython); // Expose this function to Pythonfunction js_startPython() { console.log('successful button click') return result = true; }這是我的index.html:<!DOCTYPE html><html><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Python Decider</title> <link rel="stylesheet" href="style.css"> </head><body> <div class="container"> <div class="text"> <h1> The Python Decider </h1> <p> Have a yes/no question? Click on the button to find out your answer. </p> </div> <div class="button"> <div class="flex align-midde align-center"> <a class="button glow-button" href="#" id="data">Click me!</a> </div> </div> </div> <script type="text/javascript" src="/eel.js"></script> <script type="text/javascript" src="main.js"></script> </body></html>我希望,當(dāng)我按下按鈕時(shí),python 控制臺(tái)將返回:>>> Javascript says hello! Your result is True我當(dāng)前的問(wèn)題是 JavaScript 代碼在未被調(diào)用的情況下運(yùn)行。不按任何東西,它顯示>>> Javascript says hello! Your result is True # before button is even pressed很抱歉這篇文章很長(zhǎng)。我感謝任何答案。如果這個(gè)問(wèn)題已經(jīng)在 SO 上被問(wèn)到,我也很抱歉,但我很確定它還沒(méi)有被問(wèn)到。
Python Eel - Javascript 過(guò)早運(yùn)行
慕絲7291255
2023-03-18 17:02:49