wxpython图形界面开发-Python体验(10)-图形界面之计算器

importwxclassForm(wx.Frame):def __init__( self, parent, id, title ):

wx.Frame.__init__(self,parent,id,title,wx.DefaultPosition,wx.Size(300, 250))

self.formula=False

menuBar=wx.MenuBar()

mnuFile=wx.Menu()

mnuFile.Append(22, "&Quit", "Exit Calculator")

menuBar.Append( mnuFile,"&File")

wx.EVT_MENU( self,22, self.OnClose )

self.SetMenuBar( menuBar )

self.display= wx.TextCtrl(self, -1, "", style=wx.TE_RIGHT)

gs= wx.GridSizer(5, 4, 3, 3)

gs.AddMany(

[

(wx.Button(self,12, "-"), 0, wx.EXPAND),

(wx.Button(self,20, "Cls"), 0, wx.EXPAND),

(wx.Button(self,21, "Bck"), 0, wx.EXPAND),

(wx.StaticText(self,-1, ""), 0, wx.EXPAND),

(wx.Button(self,22, "Close"), 0, wx.EXPAND),

(wx.Button(self,1, "7"), 0, wx.EXPAND),

(wx.Button(self,2, "8"), 0, wx.EXPAND),

(wx.Button(self,3, "9"), 0, wx.EXPAND),

(wx.Button(self,4, "/"), 0, wx.EXPAND),

(wx.Button(self,5, "4"), 0, wx.EXPAND),

(wx.Button(self,6, "5"), 0, wx.EXPAND),

(wx.Button(self,7, "6"), 0, wx.EXPAND),

(wx.Button(self,8, "*"), 0, wx.EXPAND),

(wx.Button(self,10, "2"), 0, wx.EXPAND),

(wx.Button(self,11, "3"), 0, wx.EXPAND),

(wx.Button(self,9, "1"), 0, wx.EXPAND),

(wx.Button(self,16, "+"), 0, wx.EXPAND),

(wx.Button(self,15, "="), 0, wx.EXPAND),

(wx.Button(self,14, "."), 0, wx.EXPAND),

(wx.Button(self,13, "0"), 0, wx.EXPAND)

]

)

sizer=wx.BoxSizer( wx.VERTICAL )

sizer.Add(self.display, 0, wx.EXPAND|wx.TOP|wx.BOTTOM, 4)

sizer.Add(gs,1, wx.EXPAND)

self.SetSizer(sizer)

self.Centre()

wx.EVT_BUTTON(self,20, self.OnClear)

wx.EVT_BUTTON(self,21, self.OnBackspace)

wx.EVT_BUTTON(self,22, self.OnClose)

wx.EVT_BUTTON(self,1, self.OnNumber)

wx.EVT_BUTTON(self,2, self.OnNumber)

wx.EVT_BUTTON(self,3, self.OnNumber)

wx.EVT_BUTTON(self,4, self.OnFormula)

wx.EVT_BUTTON(self,5, self.OnNumber)

wx.EVT_BUTTON(self,6, self.OnNumber)

wx.EVT_BUTTON(self,7, self.OnNumber)

wx.EVT_BUTTON(self,8, self.OnFormula)

wx.EVT_BUTTON(self,9, self.OnNumber)

wx.EVT_BUTTON(self,10, self.OnNumber)

wx.EVT_BUTTON(self,11, self.OnNumber)

wx.EVT_BUTTON(self,12, self.OnFormula)

wx.EVT_BUTTON(self,13, self.OnNumber)

wx.EVT_BUTTON(self,14, self.OnFormula)

wx.EVT_BUTTON(self,15, self.OnEqual)

wx.EVT_BUTTON(self,16, self.OnFormula)defOnClear(self, event):

self.display.Clear()defOnBackspace(self, event):

formula=self.display.GetValue()

self.display.Clear()

self.display.SetValue(formula[:-1])defOnClose(self, event):

self.Close()defOnEqual(self,event):ifself.formula:returnformula=self.display.GetValue()

self.formula=Truetry:

self.display.Clear()

output=eval(formula)

self.display.AppendText(str(output))exceptStandardError:

self.display.AppendText("Error")defOnFormula(self,event):ifself.formula:returnself.display.AppendText(event.EventObject.LabelText)defOnNumber(self,event):ifself.formula:

self.display.Clear()

self.formula=False

self.display.AppendText(event.EventObject.LabelText)classMyApp(wx.App):defOnInit(self):

frame= Form(None, -1, "Phoenix Caculator")

frame.Show(True)

self.SetTopWindow(frame)returnTrue

app=MyApp(0)

app.MainLoop()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值