简单计算器python20行代码在wxpython_基于python wxpython的简易计算器的源码(供大家学习)...

本文介绍了一个使用wxPython库开发的简单计算器应用,它包括基本的数字输入、运算符按钮(加减乘除、小数点、清除、等于等)、内存操作和科学计算功能。通过实例展示了如何创建窗口、布局控件以及处理按钮点击事件。
摘要由CSDN通过智能技术生成

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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值