-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalc_test.py
79 lines (60 loc) · 2 KB
/
Calc_test.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
import wx
from math import *
class CalcFrame(wx.Frame):
def __init__(self, title):
super(CalcFrame,self).__init__(None, title=title, size=(300,250))
self.InitUI()
self.Centre()
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=""
vbox.Add(self.textprint, flag=wx.EXPAND|wx.TOP|wx.BOTTOM, border=4)
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, proportion=1, flag=wx.EXPAND)
self.SetSizer(vbox)
def createHandler(self, button, labels):
item = "DEL AC = 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 == 'CLOSE':
self.Bind(wx.EVT_BUTTON, self.OnExit, 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):
string = self.equation
try:
target = eval(string)
self.equation = str(target)
self.textprint.SetValue(self.equation)
except SyntaxError:
dlg = wx.MessageDialog(self, u'Worrg format!', u'pls attention', wx.OK|wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destory()
def OnExit(self, event):
self.close()
if __name__ == '__main__':
app = wx.App()
CalcFrame(title='Calculator')
app.MainLoop()