1<!doctypehtml>2<html>34<head>5<metacharset="utf-8">6<scripttype="text/javascript"7src="https://cdn.jsdelivr.net/npm/brython@3.8.8/brython.min.js">8</script>9</head>1011<bodyonload="brython()">1213<scripttype="text/python">14from browser importdocument, html
1516# Construction de la calculatrice
17calc = html.TABLE()18calc <= html.TR(html.TH(html.DIV("0", id="result"), colspan=3)+19 html.TD("C"))20lines =["789/","456*","123-","0.=+"]2122calc <=(html.TR(html.TD(x)for x in line)for line in lines)2324document<= calc
2526result =document["result"] # direct acces to an element by its id
2728def action(event):29"""Handles the "click" event on a button of the calculator."""30 # The element the user clicked on is the attribute "target"of the
31 # event object
32 element = event.target33 # The text printed on the button is the element's "text" attribute
34 value = element.text35if value not in"=C":36 # update the result zone
37if result.textin["0","error"]:38 result.text= value
39else:40 result.text= result.text+ value
41 elif value =="C":42 # reset
43 result.text="0"44 elif value =="=":45 # execute the formula in result zone
46try:47 result.text=eval(result.text)48 except:49 result.text="error"5051# Associatefunctionaction() to the event "click" on all buttons
52for button indocument.select("td"):53 button.bind("click", action)54</script>555657</body>5859</html>