用python只需45行代码实现计算器

用python只需45行代码实现计算器_第1张图片

# coding:utf-8

importwx

classApp(wx.App):

def__init__(self):

wx.App.__init__(self)

self.frame = wx.Frame(None,title='计算器',size=(400,400))

self.panel = wx.Panel(self.frame)

self.text = wx.TextCtrl(self.panel,style=wx.TE_RIGHT | wx.TE_MULTILINE)

vbox = wx.BoxSizer(wx.VERTICAL)#水平尺寸器横向

vbox.Add(self.text,proportion=1,flag=wx.EXPAND | wx.ALL,border=2)

foriinrange(4):

showText="789/456*123-0.C+"

sbox = wx.BoxSizer()#垂直尺寸器默认

forjinrange(4):

self.button = wx.Button(self.panel,label=showText[i *4+ j])

self.button.Bind(wx.EVT_BUTTON,self.callback)

sbox.Add(self.button,proportion=1,flag=wx.EXPAND | wx.ALL,border=2)

vbox.Add(sbox,proportion=1,flag=wx.EXPAND | wx.ALL,border=2)

sbox = wx.BoxSizer()#垂直尺寸器默认

text ='()='

foriintext:

self.button = wx.Button(self.panel,label=i)

self.button.Bind(wx.EVT_BUTTON,self.callback)

sbox.Add(self.button,proportion=1,flag=wx.EXPAND | wx.ALL,border=2)

vbox.Add(sbox,proportion=1,flag=wx.EXPAND | wx.ALL,border=2)

self.panel.SetSizer(vbox)

self.frame.Show()

defcallback(self, event):

reset=True

value = event.GetEventObject().GetLabel()

s =self.text.GetValue()

ifvalue =='C':

self.text.SetValue("0")

return

ifvaluein"=":

self.text.SetValue(str(eval(s)))

reset=True

return

ifs =='0':

self.text.SetValue(value)

reset=False

else:

self.text.SetValue(s + value)

app = App()

app.MainLoop()

你可能感兴趣的:(用python只需45行代码实现计算器)