0

I need to run some python code when HTML button click, I try this code but when I clicked the button nothing happened. This is my code: ''' <!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>

''' What is my problem??

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
d-h
  • 46
  • 3
  • 1
    What makes you think your browser supports python? –  Dec 08 '20 at 12:15
  • @JustinEzequiel I used brython for this, you can read about this here https://brython.info/static_tutorial/en/index.html – d-h Dec 08 '20 at 12:20
  • 2
    Then you should have tagged brython and not python. –  Dec 08 '20 at 12:34

1 Answers1

1

Your code has a problem.Change it as follows

<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>

Full code

<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>
Nima
  • 323
  • 4
  • 13
  • I tried, but it did not help. Still nothing is happening... – d-h Dec 08 '20 at 13:13
  • @pddh Check out this [link](https://www.w3schools.com/code/tryit.asp?filename=GLGTIT4SKMYA) – Nima Dec 08 '20 at 14:31
  • It works, so what could be the reason it does not work on my computer with my pictures? – d-h Dec 08 '20 at 15:31
  • @pddh Do you see an error in your browser console? – Nima Dec 09 '20 at 08:44
  • Thanks, I solved the problem, you can see in my code that my function did not receive any event and that was the problem... – d-h Dec 09 '20 at 15:26