# wxPython calculator - in the python news

Pycoders has a new tutorial for wxPython.

Calculators are rather common as GUI “Hello World” programs: Tutorial Project: Calculator — wxGlade 1.0.0 documentation

Yeah…they are. But it’s a good beginner tutorial for someone new to wxPython

I have several others in my book, which is where this originally came from

A long time ago, I made a twenty-line wx code for a calculator and posted it to I made a Calculator with 20 lines of code using wx.
Now, I improved my original code to 19 lines (I learned how to use `AddMany` )

``````import wx

def pack(items, orient=wx.HORIZONTAL):
sizer = wx.BoxSizer(orient)
sizer.AddMany((i, 1, wx.EXPAND|wx.ALL, 0) for i in items)
return sizer

def bt_press(key):
if   key == 'C': disp.Value = ''
elif key == '<': disp.Value = disp.Value[:-1]
elif key == '=': disp.Value = str(round(eval(disp.Value),16))
else           : disp.Value += key

app = wx.App()
self = wx.Frame(None, title="wxcalc")

keys = '()C<789/456*123-.0=+'
btns = [[wx.Button(self, label=c) for c in keys[i:i+4]] for i in range(0,20,4)]
disp = wx.TextCtrl(self)

self.Bind(wx.EVT_BUTTON, lambda v: bt_press(v.EventObject.Label))
self.SetSizer(pack([disp] + [pack(x) for x in btns], orient=wx.VERTICAL))
self.Show()
app.MainLoop()
``````