我正在学习 pyscript,我正在尝试制作一个单词游戏。到目前为止,一切进展顺利,除了我收到此“ImportError:无法从 'pyodide' (/lib/python311.zip/pyodide/init.py) 导入名称 'create_proxy'” 错误。
这是我的代码:
<html>
<head>
<style>
body{
margin: 0px;
padding: 0px;
background-color: #011627;
color: #fdfffc;
text-align: center;
background-image: url(https://cdn.pixabay.com/photo/2015/07/02/10/13/sky-828648_960_720.jpg);
background-position: center;
background-repeat: no-repeat;
background-size: cover;
height: 100vh;
}
h1{
font-size: 65px;
font-family: 'Comfortaa';
}
input{
width: 75px;
height: 55px;
border-radius: 10px;
font-size: 44px;
margin: 10px;
}
.btn:hover{
background-color:green;
cursor: pointer;
}
</style>
<link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
<script defer src="https://pyscript.net/latest/pyscript.js"></script>
</head>
<body>
<p>Guess a word (5 letters)</p>
<p id="guesses">Number of guesses: 0</p>
<input type="text" id="guess" name="guess" style="width: 370px; height: 70px; background-color: #4c555c;color: white;"/><br /><br />
<input type="button" id="guessBTN" name="guess" style="width: 750px; height: 70px; background-color: #0091ff;color: white;"/>
<p id="colors"></p>
<py-script>
from js import document
from pyodide import create_proxy
#for each guess w, compare with last word(ans)
#list of result
ans = robot
tries = 6
def guess(current):
currentL = list(current)
List_Of_Ans = [0, 0, 0, 0, 0]
for s in range(len(List_Of_Ans)): #letter is the letter that we are testing first one is R Route
listAns = list(ans)
Rletter = listAns[s]
Tletter = currentL[s]
if (Rletter == Tletter):
List_Of_Ans[s] = 2
#print(List_Of_Ans)
listAns.remove(Rletter)
#print(listAns)
#print()
for s in range(len(List_Of_Ans)): #letter is the letter that we are testing first one is R Route
if List_Of_Ans[s] == 2:
continue
Tletter = currentL[s]
#print(s, Tletter)
if(Tletter in listAns):
List_Of_Ans[s] = 1
#print(List_Of_Ans)
#print(listAns)
listAns.remove(Tletter)
#print(listAns)
for z in range(len(List_Of_Ans)):
if(List_Of_Ans[z] == 1):
List_Of_Ans[z] = "YELLOW"
if(List_Of_Ans[z] == 2):
List_Of_Ans[z] = "GREEN"
if(List_Of_Ans[z] == 0):
List_Of_Ans[z] = "GRAY"
pyscript.write(List_Of_Ans[0], List_Of_Ans[1], List_Of_Ans[2], List_Of_Ans[3], List_Of_Ans[4])
if (List_Of_Ans == ["GREEN", "GREEN", "GREEN", "GREEN", "GREEN"]):
pyscript.write(f"YAY you got it in {x+1} tries !!!")
guess_proxy = create_proxy(guess)
guess_button = document.getElementById("guessBTN")
guess_button.addEventListener("click", guess_proxy)
</py-script>
</body>
</html>
很简单, 我会虚心接受建议
我正在尝试制作一个小文字游戏
我现在只想不要收到此错误,稍后我会尝试添加更多功能。
谢谢你
您需要从
create_proxy
导入 pyodide.ffi
。
你的代码应该变成:
<html>
<head>
<link href="https://pyscript.net/releases/2024.1.1/core.css" rel="stylesheet"/>
<script src="https://pyscript.net/releases/2024.1.1/core.js"></script>
<link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
<script defer src="https://pyscript.net/latest/pyscript.js"></script>
</head>
<body>
<button>Click Me!</button>
<py-script>
from js import document
from pyodide.ffi import create_proxy
element = document.querySelector("button")
def button_click(event):
print("Button clicked!", event)
click_proxy = create_proxy(button_click)
element.addEventListener("click", click_proxy)
</py-script>
</body>
</html>