from __future__ import division
import ipywidgets as ipw
output = ipw.Text(
placeholder="0", layout=ipw.Layout(width="190px"), disabled=True
)
def on_click(btn):
if btn.description == "=":
try:
output.value = str(eval(output.value))
except:
output.value = "ERROR"
elif btn.description == "AC":
output.value = ""
elif btn.description == "del":
output.value = output.value[:-1]
else:
output.value = output.value + btn.description
def mk_btn(description):
btn = ipw.Button(description=description, layout=ipw.Layout(width="45px"))
btn.on_click(on_click)
return btn
row0 = ipw.HBox([mk_btn(d) for d in ("(", ")", "del", "AC")])
row1 = ipw.HBox([mk_btn(d) for d in ("7", "8", "9", " / ")])
row2 = ipw.HBox([mk_btn(d) for d in ("4", "5", "6", " * ")])
row3 = ipw.HBox([mk_btn(d) for d in ("1", "2", "3", " - ")])
row4 = ipw.HBox([mk_btn(d) for d in ("0", ".", "=", " + ")])
ipw.VBox((output, row0, row1, row2, row3, row4))