这是我的 HTML 代码:
<!DOCTYPE html>
<html lang="en" class="light">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ScratchFind</title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://pyscript.net/releases/2024.4.1/core.css">
<script type="module" src="https://pyscript.net/releases/2024.4.1/core.js"></script>
<script type="py" src="./main.py" config="./pyscript.json"></script>
</head>
<body>
<!-- Back end -->
<script src="script.js"></script>
<!-- Front end -->
<h1>ScratchFind</h1>
<input id="url" type="text" name="input" placeholder="Paste your URL here">
<input py-click="hello" type="submit" value="Find!">
<h1 id="display"></h1>
<!-- Theme toggle -->
<label class="switch">
<input type="checkbox" onclick="toggleTheme()">
<span class="slider"></span>
</label>
</body>
</html>
这是我的Python代码:
import requests
from pyscript import document
url = document.querySelector("#url")
display = document.querySelector("#display")
display.innerText = "a"
link = ""
def hello():
link = url.value
display.innerText = link
main.py 正在工作,但当我单击按钮时,该函数没有被调用。
我期望当按下按钮时,hello 函数中的代码可以工作。
当我尝试你的代码时,我看到一个错误:
TypeError: hello() takes 0 positional arguments but 1 was given
使用
py-*event
语法时,函数会传递与触发事件相对应的 Event 对象。您可以接收此事件并使用它 (def hello(evt):
) 或吞下并忽略它 (def hello(*args)
)。