您的位置:首页 > 编程语言 > Python开发

基于python wxpython的简易计算器的源码(供大家学习)

2010-09-29 15:38 435 查看
import wx
import math
class CalculatorFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None,-1,'PyCalc')
self.panel=wx.Panel(self,-1)
self.sizer=wx.GridBagSizer(1,1)
self.display=wx.TextCtrl(self.panel,-1,'0.',style=wx.TE_READONLY|wx.TE_RIGHT)
self.display.SetMaxLength(5)
self.sizer.Add(self.display,(0,0),(1,5),wx.EXPAND)
self.memoryDisplay=wx.StaticText(self.panel,-1,'0',style=wx.ALIGN_CENTER)
self.sizer.Add(self.memoryDisplay,(4,0),(1,1),wx.ALIGN_CENTER)
self.sizer.Add(wx.Button(self.panel,107,'Clear',size=(30,30)),(5,0),(1,2),wx.EXPAND)
wx.EVT_BUTTON(self.panel,107,self.handler)
buttons=[[None,None,None,None,None],/
[['M+',100],['1',1],['2',2],['3',3],['+',200]],/
[['M-',101],['4',4],['5',5],['6',6],['-',201]],/
[['MR',102],['7',7],['8',8],['9',9],['*',202]],/
[None,['.',103],['0',0],['=',104],['/',203]],/
[None,None,['B',105],['+/-',106],['sqrt',204]]]
x=y=0
for row in buttons:
for button in row:
if button==None:
x=x+1
continue
self.sizer.Add(wx.Button(self.panel,button[1],button[0],size=(30,30)),(y,x))
wx.EVT_BUTTON(self.panel,button[1],self.handler)
x=x+1
x=0
y=y+1
self.memory=0
self.last=None
self.operation=None
self.panel.SetSizerAndFit(self.sizer)
self.SetClientSize(self.panel.GetSize())
self.Show(True)
def handler(self,event):
id=event.GetId()
if(id>=0)&(id<=9):
if self.display.GetValue()=='0.':
self.display.SetValue('')
self.display.AppendText(str(id))
elif id==100:
self.memory=float(self.display.GetValue())
elif id==101:
self.memory=0
elif id==102:
if self.memory!=0:
self.display.SetValue(str(self.memory))
elif id==103:
if self.display.GetValue().find('.')==-1:
self.display.AppendText('.')
elif id==104:
self.solve()
elif id==105:
if len(self.display.GetValue())>1:
self.display.SetValue(self.display.GetValue()[:-1])
elif len(self.display.GetValue())==1:
self.display.SetValue('0.')
elif id==106:
self.display.SetValue(str(float(self.display.GetValue())*-1))
elif id==107:
self.display.SetValue('0.')
self.last=None
self.operation=None
elif id==200:
self.solve()
self.last=self.display.GetValue()
self.operation='+'
self.display.SetValue('0.')
elif id==201:
self.solve()
self.last=self.display.GetValue()
self.operation='-'
self.display.SetValue('0.')
elif id==202:
self.solve()
self.last=self.display.GetValue()
self.operation='*'
self.display.SetValue('0.')
elif id==203:
self.solve()
self.last=self.display.GetValue()
self.operation='/'
self.display.SetValue('0.')
elif id==204:
if float(self.display.GetValue())>0:
self.display.SetValue(str(math.sqrt(float(self.display.GetValue()))))
def solve(self):
if(self.last!=None)&(self.operation!=None):
self.display.SetValue(str(eval(str(self.last)+self.operation+str(self.display.GetValue()))))
self.last=None
self.operation=None

calculator=wx.PySimpleApp()
CalculatorFrame()
calculator.MainLoop()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: