Calculator Demo

Show a small calculator demo in 50 lines of code.

The Code

   1 '''wxPython Calculator Demo in 50 lines of code'''
   2 
   3 # Calculator GUI:
   4 
   5 # ___________v
   6 # [7][8][9][/] 
   7 # [4][5][6][*]
   8 # [1][2][3][-]
   9 # [0][.][C][+]
  10 # [    =     ]
  11 
  12 from __future__ import division # So that 8/3 will be 2.6666 and not 2
  13 import wx
  14 from math import * # So we can evaluate "sqrt(8)"
  15 
  16 class Calculator(wx.Dialog):
  17     '''Main calculator dialog'''
  18     def __init__(self):
  19         wx.Dialog.__init__(self, None, -1, "Calculator")    
  20         sizer = wx.BoxSizer(wx.VERTICAL) # Main vertical sizer
  21 
  22         # ____________v
  23         self.display = wx.ComboBox(self, -1) # Current calculation
  24         sizer.Add(self.display, 0, wx.EXPAND) # Add to main sizer
  25 
  26         # [7][8][9][/] 
  27         # [4][5][6][*]
  28         # [1][2][3][-]
  29         # [0][.][C][+]
  30         gsizer = wx.GridSizer(4, 4)
  31         for row in (("7", "8", "9", "/"),
  32                     ("4", "5", "6", "*"),
  33                     ("1", "2", "3", "-"),
  34                     ("0", ".", "C", "+")):
  35             for label in row:
  36                 b = wx.Button(self, -1, label)
  37                 gsizer.Add(b)
  38                 self.Bind(wx.EVT_BUTTON, self.OnButton, b)
  39         sizer.Add(gsizer, 1, wx.EXPAND)
  40 
  41         # [    =     ]
  42         b = wx.Button(self, -1, "=")
  43         self.Bind(wx.EVT_BUTTON, self.OnButton, b)
  44         sizer.Add(b, 0, wx.EXPAND)
  45         self.equal = b
  46 
  47         # Set sizer and center
  48         self.SetSizer(sizer)
  49         sizer.Fit(self)
  50         self.CenterOnScreen()
  51 
  52     def OnButton(self, evt):
  53         '''Handle button click event'''
  54         # Get title of clicked button
  55         label = evt.GetEventObject().GetLabel() 
  56 
  57         if label == "=": # Calculate
  58             try:
  59                 compute = self.display.GetValue()
  60                 # Ignore empty calculation
  61                 if not compute.strip():
  62                     return
  63 
  64                 # Calculate result
  65                 result = eval(compute)
  66 
  67                 # Add to history
  68                 self.display.Insert(compute, 0)
  69                 
  70                 # Show result
  71                 self.display.SetValue(str(result))
  72             except Exception, e:
  73                 wx.LogError(str(e))
  74                 return
  75 
  76         elif label == "C": # Clear
  77             self.display.SetValue("")
  78 
  79         else: # Just add button text to current calculation
  80             self.display.SetValue(self.display.GetValue() + label)
  81             self.equal.SetFocus() # Set the [=] button in focus
  82 
  83 if __name__ == "__main__":
  84     # Run the application
  85     app = wx.PySimpleApp()
  86     dlg = Calculator()
  87     dlg.ShowModal()
  88     dlg.Destroy()

MikiTebeka

CalculatorDemo (last edited 2008-03-11 10:50:20 by localhost)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.