因此,我尝试为我的网页创建一个按钮。该按钮的功能是从Raspberry Pi运行Python脚本。我试着按照CgiScript指南来做。但是当我点击这个按钮的时候,它只显示网页上的脚本,而不是从Pi上的Thonny IDLE运行它。 这是我的HTML代码
<!DOCTYPE HTML>
<html>
<head>
<title>Twisted Twister</title>
<link rel="shortcut icon" href="favicon.png">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="Content-Language" content="nl-nl">
<meta http-equiv="Content-Language" content="en-gb">
<meta http-equiv="content-type" content="text/html; charset=WINDOWS-1252" >
<style>
body {
font-family: Arial, Helvetica, sans-serif;
font-size: 30px;
color: #FFB0FE
background url("texture.png")
}`
#fixed {
position: fixed;
top: 0;
left: 0;
}
#arrowPad {
border: 10px #FFFFFF;
}
#USERDATA {
background: 10px #FFFFFF;
}
#USERDATA.h {
background: 10px #FFFFFF;
}
#USERDATA.h:hover {
background: 10px #FFFFFF;
}
</style>
<body>
<>
<div id="USERDATA"></div>
<script type="text/javascript" src="jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
setInterval(function () {
$('#USERDATA').load('RequestUserData.php')
}, 200);
})
</table>
</script>
<table>
<div id="PI"></div>
<script type="text/javascript" src="jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
setInterval(function () {
$('#PI').load('RequestPiData.php')
}, 200);
});
</script>
</table>
<div id="fixed">
<table id="arrowPad">
<tr>
<td id="USERDATA" class="h"><a href="/usr/lib/cgi-
bin/aantalSpelers2.py" ><img src="https://imgflip.com/s/meme/Blank-Nut-
Button.jpg" width="150" height="150" id="a2" alt="PythonScript"></img></a>
</td>
</tr>
</table>
</div>
</body>
</html>
这是我的Python脚本 def main():
# 0 changes to 1 when players are joining the game
speler_een = 0
speler_twee = 0
speler_drie = 0
speler_vier = 0
# This function makes an inventory of how many players are involved
class HoeveelSpelers:
def verkrijg_aantal_spelers(spelers_totaal):
hoeveelSpelers = int(input('Hoeveel spelers spelen er mee? (1-4) '))
while (hoeveelSpelers > 4 or hoeveelSpelers < 1):
print('Dit is niet een geldige hoeveelheid spelers. Probeer het
opnieuw. ')
hoeveelSpelers = int(input('Hoeveel spelers spelen er mee? (1-4)
'))
return hoeveelSpelers
h = HoeveelSpelers()
spelers_totaal = h.verkrijg_aantal_spelers()
if (spelers_totaal == 4):
speler_een = 1
speler_twee = 1
speler_drie = 1
speler_vier = 1
elif (spelers_totaal == 3):
speler_een = 1
speler_twee = 1
speler_drie = 1
elif (spelers_totaal == 2):
speler_een = 1
speler_twee = 1
elif (spelers_totaal == 1):
speler_een = 1
print(speler_een, speler_twee, speler_drie, speler_vier)
main()
所以基本上我想做的就是点击网页上的按钮并运行脚本。你知道吗
从HTML调用Python代码并不像创建指向文件路径的超链接那么容易(但一旦您了解了基本知识,也不会太复杂)。你知道吗
为了使Python代码可以从HTML页面访问,您需要在后面运行一个适当的pythonweb服务器(例如Flask)。你知道吗
坚持使用Flask示例-如果您想拥有一个触发某个函数(例如
foo()
)的按钮,您需要注册一个将URL映射到该函数的“端点”(示例取自here):后端
假设您在与前端相同的主机上的端口5000上运行Flask服务器,则按钮需要链接到
前端
如果没有返回值,实际的处理只在后端进行,而前端没有注意到。当涉及到返回值时(我假设你的例子就是这样),事情变得更具挑战性。我只能推荐Flask Mega Tutorial (by Miguel Grinberg)。它涵盖了许多基础知识和高级功能,这些功能可能会帮助您规划我们的应用程序。你知道吗
希望这有帮助。。。你知道吗
相关问题 更多 >
编程相关推荐