0

单击 HTML 按钮时我需要运行一些 python 代码,我尝试使用此代码,但是当我单击按钮时没有任何反应。这是我的代码:''' <!doctype html>

<html>

<head>
    <meta charset="utf-8">
    <script type="text/javascript"
        src="https://cdn.jsdelivr.net/npm/brython@3.9.0/brython.min.js">
    </script>
</head>

<body onload="brython()">

    <script type="text/python">
        def change_image():
            from browser import document, html
            document["image"].clear()
            document["image"] <= html.IMG(src="flowers.jpg")

        document["next"].bind("click", change_image)

    </script>
    <img id="image" src="image.png">
    <input id="next" type="button" value="next state"/>


</body>

</html>

'''我的问题是什么??

4

1 回答 1

0

你的代码有问题,改如下

<script type="text/python">

from browser import document, html #Use the form outside the function
def change_image(item):
    document["image"].clear()
    document["image"].src="flowers.jpg" #change attributes

document["next"].bind("click", change_image)

</script>

完整代码

<head>
    <meta charset="utf-8">
    <script type="text/javascript"
        src="https://cdn.jsdelivr.net/npm/brython@3.9.0/brython.min.js">
    </script>
</head>

<body onload="brython()">
    <script type="text/python">

from browser import document, html
def change_image(item):
    document["image"].clear()
    document["image"].src="https://www.w3schools.com/html/img_chania.jpg"

document["next"].bind("click", change_image)

    </script>

    <img width=200 id="image" src="https://www.w3schools.com/html/pic_trulli.jpg">
    <input id="next" type="button" value="next state"/>
</body>

于 2020-12-08T13:00:52.653 回答