source: http://blog.163.com/lxq_102172/blog/static/133398015201231012213428/
# -*- coding: utf-8 -*-
from __future__ import division
import wx
import math
labels='1 1 1 1 1 MC MR MS M+ M- <- CE C +- Sqr 7 8 9 / % 4 5 6 * 1/x 1 2 3 - 0 0 . . + 2'.split()
class CalFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None, -1, 'Calculator', size=(230,310),
style=wx.DEFAULT_FRAME_STYLE ^ (wx.RESIZE_BORDER | wx.MINIMIZE_BOX |wx.MAXIMIZE_BOX))
panel=wx.Panel(self)
panel.SetBackgroundColour('White')
self.createMenubar()
self.createButtons(panel)
def createMenubar(self):
menuBar=wx.MenuBar()
menu1=wx.Menu()
menuBar.Append(menu1,"View")
menu2=wx.Menu()
menuBar.Append(menu2,"Edit")
menu3=wx.Menu()
menuBar.Append(menu3,"Help")
self.SetMenuBar(menuBar)
def createButtons(self,panel):
sizer = wx.GridBagSizer(hgap=5, vgap=5)#grid sizer
self.text=wx.TextCtrl(panel, wx.NewId(),value='0', style=wx.TE_READONLY |
wx.TE_RIGHT | wx.TE_NOHIDESEL, size=(50,50))
sizer.Add(self.text,pos=(0,0),span=(1,5),flag=wx.EXPAND)
i=0
for row in range(1,7):
for col in range(5):
i=i+1
self.button=wx.Button(panel, label=str(labels[row*5+col]),size=(40,30), id=i)
self.button.Bind(wx.EVT_BUTTON, self.Handler, self.button)
sizer.Add(self.button,pos=(row,col))
sizer.Detach(25)
sizer.Detach(29)
sizer.Detach(25)
sizer.Detach(25)
self.button=wx.Button(panel, label='=',size=(40,60), id=30)
self.button.Bind(wx.EVT_BUTTON,self.Handler, self.button)
sizer.Add(self.button,pos=(5,4),span=(2,1),flag=wx.EXPAND)
self.button=wx.Button(panel,label='0',id=25)
self.button.Bind(wx.EVT_BUTTON,self.Handler, self.button)
sizer.Add(self.button,pos=(6,0),span=(1,2),flag=wx.EXPAND)
panel.SetSizer(sizer)
panel.Fit()
self.operand1=None
self.operand2=None
self.operator=None
self.temp1=0
def Handler(self,event):
Id=event.GetId()
if Id in [11,12,13,16,17,18,21,22,23,25]:
if self.text.GetValue()=='0':
self.text.SetValue('')
self.text.AppendText(event.GetEventObject().GetLabel())
else:
self.text.SetValue(self.text.GetValue()+event.GetEventObject().GetLabel())
elif Id==28: #.
if '.' in self.text.GetValue()[(self.Index()+1):]:
self.text.SetValue(self.text.GetValue())
else:
self.text.SetValue(self.text.GetValue()+event.GetEventObject().GetLabel())
elif Id==8: #C
self.text.SetValue('0')
self.operand1=None
self.operand2=None
self.operator=None
self.temp1=0
elif Id==7: #CE
self.text.SetValue(self.text.GetValue()[0:(self.Index()+1)])
elif Id==6: #<-
self.text.SetValue(self.text.GetValue()[0:-1])
if len(self.text.GetValue())<=0:
self.text.SetValue('0')
elif Id==9: #+-
if self.text.GetValue()=='0':
pass
else:
if self.text.GetValue()[0]!='-' & self.operand2==None:
self.text.SetValue('-'+self.text.GetValue())
else:
self.text.SetValue(self.text.GetValue()[1:])
if self.operand2!=None:
pass
elif Id==10: #Sqr
try:
self.text.SetValue(str(math.sqrt(float(self.text.GetValue()))))
except ValueError:
self.text.SetValue('ValueError:math domain error')
elif Id==29: #+
self.temp1=self.temp1+1
self.operator='+'
self.Repeat()
elif Id==24: #-
self.temp1=self.temp1+1
self.operator='-'
self.Repeat()
elif Id==19: #*
self.temp1=self.temp1+1
self.operator='*'
self.Repeat()
elif Id==14: #/
self.temp1=self.temp1+1
self.operator='/'
self.Repeat()
elif Id==20: #1/x
try:
self.text.SetValue(str(1.0/(float(self.text.GetValue()))))
except ZeroDivisionError:
self.text.SetValue('ZeroDivisionError: float division by zero')
elif Id==15: #%
self.operand2=eval(self.operand1) * eval(self.operand2)/100.0
self.operand2=str(self.operand2)
self.Result()
self.temp1=0
elif Id==30: #=
self.Result()
self.temp1=0
if self.temp1>=2:
try:
if self.operand2!=None and self.operand2!='' and self.operand1!=None and self.operator!=None:
self.text.SetValue(str(eval(self.operand1 + self.operator + self.operand2)))
self.operand1=self.text.GetValue()
self.operand2=None
else:
pass
except ZeroDivisionError:
self.text.SetValue('ZeroDivisionError: float division by zero')
self.Repeat()
self.temp1=1
if self.temp1%2==0:
if self.operator==None:
self.operand1=self.text.GetValue()
else:
self.operand1=self.text.GetValue()[0:self.Index()]
else:
self.operand2=self.text.GetValue()[(self.Index()+1):]
print self.operand1,self.operator, self.operand2
def Index(self): #���ز������±�
a=list(self.text.GetValue())
if self.operator==None or self.operator not in a:
b=-1
else:
b=a.index(self.operator)
return b
def Repeat(self):
if self.text.GetValue()[-1] in '+-*/':
self.text.SetValue(self.text.GetValue()[0:self.Index()]
+self.operator)
else:
self.text.SetValue(self.text.GetValue()+self.operator)
def Result(self):
try:
if self.operand1!=None and self.operator!=None:
self.text.SetValue(str(eval(self.operand1 + self.operator + self.operand2)))
self.operand1=None
self.operand2=None
self.operator=None
self.temp1=0
except ZeroDivisionError:
self.text.SetValue('ZeroDivisionError: float division by zero')
app=wx.PySimpleApp()
frame=CalFrame()
frame.Show()
app.MainLoop()