TA的每日心情 | 开心 2016-10-18 06:23 |
---|
签到天数: 72 天 连续签到: 1 天 [LV.6]常住居民II
|
地板
楼主 |
发表于 2016-2-3 21:28:31
|
只看该作者
Python代码 收藏代码
1.#!/usr/bin/env python
2.# FileName: calculator.py
3.import wx
4.class MyFrame( wx.Frame ):
5.def __init__( self, parent, id, title ):
6.wx.Frame.__init__(self,parent,id,title,wx.DefaultPosition,wx.Size(300, 250))
7.
8.self.formula = False
9.
10.menubar = wx.MenuBar()
11.file = wx.Menu()
12.file.Append( 22, '&Quit', 'Exit Calculator' )
13.menubar.Append( file, '&File' )
14.self.SetMenuBar( menubar )
15.
16.wx.EVT_MENU( self, 22, self.OnClose )
17.sizer = wx.BoxSizer( wx.VERTICAL )
18.
19.self.display = wx.TextCtrl(self, -1, '', style=wx.TE_RIGHT)
20.sizer.Add(self.display, 0, wx.EXPAND|wx.TOP|wx.BOTTOM, 4)
21.gs = wx.GridSizer(4, 4, 3, 3)
22.gs.AddMany([(wx.Button(self, 20, 'Cls'), 0, wx.EXPAND),
23.(wx.Button(self, 21, 'Bck'), 0, wx.EXPAND),
24.(wx.StaticText(self, -1, ''), 0, wx.EXPAND),
25.(wx.Button(self, 22, 'Close'), 0, wx.EXPAND),
26.(wx.Button(self, 1, '7'), 0, wx.EXPAND),
27.(wx.Button(self, 2, '8'), 0, wx.EXPAND),
28.(wx.Button(self, 3, '9'), 0, wx.EXPAND),
29.(wx.Button(self, 4, '/'), 0, wx.EXPAND),
30.(wx.Button(self, 5, '4'), 0, wx.EXPAND),
31.(wx.Button(self, 6, '5'), 0, wx.EXPAND),
32.(wx.Button(self, 7, '6'), 0, wx.EXPAND),
33.(wx.Button(self, 8, '*'), 0, wx.EXPAND),
34.(wx.Button(self, 9, '1'), 0, wx.EXPAND),
35.(wx.Button(self, 10, '2'), 0, wx.EXPAND),
36.(wx.Button(self, 11, '3'), 0, wx.EXPAND),
37.(wx.Button(self, 12, '-'), 0, wx.EXPAND),
38.(wx.Button(self, 13, '0'), 0, wx.EXPAND),
39.(wx.Button(self, 14, '.'), 0, wx.EXPAND),
40.(wx.Button(self, 15, '='), 0, wx.EXPAND),
41.(wx.Button(self, 16, '+'), 0, wx.EXPAND)])
42.sizer.Add(gs, 1, wx.EXPAND)
43.self.SetSizer(sizer)
44.self.Centre()
45.wx.EVT_BUTTON(self, 20, self.OnClear)
46.wx.EVT_BUTTON(self, 21, self.OnBackspace)
47.wx.EVT_BUTTON(self, 22, self.OnClose)
48.wx.EVT_BUTTON(self, 1, self.OnSeven)
49.wx.EVT_BUTTON(self, 2, self.OnEight)
50.wx.EVT_BUTTON(self, 3, self.OnNine)
51.wx.EVT_BUTTON(self, 4, self.OnDivide)
52.wx.EVT_BUTTON(self, 5, self.OnFour)
53.wx.EVT_BUTTON(self, 6, self.OnFive)
54.wx.EVT_BUTTON(self, 7, self.OnSix)
55.wx.EVT_BUTTON(self, 8, self.OnMultiply)
56.wx.EVT_BUTTON(self, 9, self.OnOne)
57.wx.EVT_BUTTON(self, 10, self.OnTwo)
58.wx.EVT_BUTTON(self, 11, self.OnThree)
59.wx.EVT_BUTTON(self, 12, self.OnMinus)
60.wx.EVT_BUTTON(self, 13, self.OnZero)
61.wx.EVT_BUTTON(self, 14, self.OnDot)
62.wx.EVT_BUTTON(self, 15, self.OnEqual)
63.wx.EVT_BUTTON(self, 16, self.OnPlus)
64.
65.def OnClear(self, event):
66.self.display.Clear()
67.def OnBackspace(self, event):
68.formula = self.display.GetValue()
69.self.display.Clear()
70.self.display.SetValue(formula[:-1])
71.def OnClose(self, event):
72.self.Close()
73.def OnDivide(self, event):
74.if self.formula:
75.return
76.self.display.AppendText('/')
77.def OnMultiply(self, event):
78.if self.formula:
79.return
80.self.display.AppendText('*')
81.def OnMinus(self, event):
82.if self.formula:
83.return
84.self.display.AppendText('-')
85.def OnPlus(self, event):
86.if self.formula:
87.return
88.self.display.AppendText('+')
89.def OnDot(self, event):
90.if self.formula:
91.return
92.self.display.AppendText('.')
93.def OnEqual(self, event):
94.if self.formula:
95.return
96.formula = self.display.GetValue()
97.self.formula = True
98.try:
99.self.display.Clear()
100.output = eval(formula)
101.self.display.AppendText(str(output))
102.except StandardError:
103.self.display.AppendText("Error")
104.def OnZero(self, event):
105.if self.formula:
106.self.display.Clear()
107.self.formula = False
108.self.display.AppendText('0')
109.def OnOne(self, event):
110.if self.formula:
111.self.display.Clear()
112.self.formula = False
113.self.display.AppendText('1')
114.def OnTwo(self, event):
115.if self.formula:
116.self.display.Clear()
117.self.formula = False
118.self.display.AppendText('2')
119.def OnThree(self, event):
120.if self.formula:
121.self.display.Clear()
122.self.formula = False
123.self.display.AppendText('3')
124.def OnFour(self, event):
125.if self.formula:
126.self.display.Clear()
127.self.formula = False
128.self.display.AppendText('4')
129.def OnFive(self, event):
130.if self.formula:
131.self.display.Clear()
132.self.formula = False
133.self.display.AppendText('5')
134.def OnSix(self, event):
135.if self.formula:
136.self.display.Clear()
137.self.formula = False
138.self.display.AppendText('6')
139.def OnSeven(self, event):
140.if self.formula:
141.self.display.Clear()
142.self.formula = False
143.self.display.AppendText('7')
144.def OnEight(self, event):
145.if self.formula:
146.self.display.Clear()
147.self.formula = False
148.self.display.AppendText('8')
149.def OnNine(self, event):
150.if self.formula:
151.self.display.Clear()
152.self.formula = False
153.self.display.AppendText('9')
154.
155.class MyApp(wx.App):
156.def OnInit(self):
157.frame = MyFrame(None, -1, "calculator.py")
158.frame.Show(True)
159.self.SetTopWindow(frame)
160.return True
161.app = MyApp(0)
162.app.MainLoop()
我们输入的公式使用 python 的内置函数 eval 来处理。
output = eval( formula )
如果公式有错,就会显示一条错误信息。请注意我们是如何在 Bck 和 Close 按纽之间插入空白的。我们只是简单的在那放了一个空的 wx.StaticText。这是一个很常用的技巧。 |
|