-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathre_calculator.py
84 lines (71 loc) · 2.66 KB
/
re_calculator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import wx
import math
class ClacFrame(wx.Frame):
def __init__(self, title):
super().__init__(None, title=title, size=(300, 260))
self.InitUI()
self.Center()
self.Show()
def InitUI(self):
vbox = wx.BoxSizer(wx.VERTICAL)
self.textprint = wx.TextCtrl(self, -1, '', style=wx.TE_RIGHT|wx.TE_READONLY)
self.equation = ''
self.textprint.SetValue(self.equation)
vbox.Add(self.textprint, flag=wx.EXPAND|wx.TOP|wx.BOTTOM, border=5)
gridBox = wx.GridSizer(5, 4, 5, 5)
labels=['AC', 'DEL', 'PI', 'CLOSE', '7', '8', '9', '/',
'4', '5', '6', '*', '1', '2', '3', '-', '0', '.',
'=', '+']
for label in labels:
buttonItem = wx.Button(self, label=label)
self.createHandler(buttonItem, label)
gridBox.Add(buttonItem, 1, wx.EXPAND)
vbox.Add(gridBox, 1, flag=wx.EXPAND)
self.SetSizer(vbox)
def createHandler(self, button, labels):
item = 'DEL AC = PI CLOSE'
if labels not in item:
self.Bind(wx.EVT_BUTTON, self.OnAppend, button)
elif labels == 'DEL':
self.Bind(wx.EVT_BUTTON, self.OnDel, button)
elif labels == 'AC':
self.Bind(wx.EVT_BUTTON, self.OnAc, button)
elif labels == '=':
self.Bind(wx.EVT_BUTTON, self.OnTarget, button)
elif labels == 'PI':
self.Bind(wx.EVT_BUTTON, self.OnPi, button)
elif labels == 'CLOSE':
self.Bind(wx.EVT_BUTTON, self.OnClose, button)
def OnAppend(self, event):
eventButton = event.GetEventObject()
label = eventButton.GetLabel()
self.equation += label
self.textprint.SetValue(self.equation)
def OnDel(self, event):
self.equation = self.equation[:-1]
self.textprint.SetValue(self.equation)
def OnAc(self, event):
self.textprint.Clear()
self.equation = ''
def OnTarget(self, event):
try:
self.equation = str(eval(self.equation))
self.textprint.SetValue(self.equation)
except SyntaxError:
dlg = wx.MessageDialog(self, '格式错误', '警告', wx.OK|wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
def OnPi(self, event):
if self.equation == '':
self.equation = str(math.pi)
elif self.equation == str(math.pi):
self.textprint.Clear()
else:
self.equation += str(math.pi)
self.textprint.SetValue(self.equation)
def OnClose(self, event):
self.Close()
if __name__ == '__main__':
app = wx.App()
ClacFrame(title='Claculator')
app.MainLoop()