def OnKeyTyped(self, event): #直接键盘输入
self.calculation = event.GetString()
'''
定义每一个按钮的操作
'''
def one(self, event):
self.calculation = self.calculation + "1"
self.inputField.SetValue(self.calculation)
def two(self, event):
self.calculation = self.calculation + "2"
self.inputField.SetValue(self.calculation)
def three(self, event):
self.calculation = self.calculation + "3"
self.inputField.SetValue(self.calculation)
def four(self, event):
self.calculation = self.calculation + "4"
self.inputField.SetValue(self.calculation)
def five(self, event):
self.calculation = self.calculation + "5"
self.inputField.SetValue(self.calculation)
def six(self, event):
self.calculation = self.calculation + "6"
self.inputField.SetValue(self.calculation)
def seven(self, event):
self.calculation = self.calculation + "7"
self.inputField.SetValue(self.calculation)
def eight(self, event):
self.calculation = self.calculation + "8"
self.inputField.SetValue(self.calculation)
def nine(self, event):
self.calculation = self.calculation + "9"
self.inputField.SetValue(self.calculation)
def zero(self, event):
self.calculation = self.calculation + "0"
self.inputField.SetValue(self.calculation)
def clear(self, event):
self.calculation = ""
self.inputField.SetValue(self.calculation)
def plus(self, event):
self.calculation = self.calculation + "+"
self.inputField.SetValue(self.calculation)
def minus(self, event):
self.calculation = self.calculation + "-"
self.inputField.SetValue(self.calculation)
def multiply(self, event):
self.calculation = self.calculation + "*"
self.inputField.SetValue(self.calculation)
def divide(self, event):
self.calculation = self.calculation + "/"
self.inputField.SetValue(self.calculation)
def point(self, event):
self.calculation = self.calculation + "."
self.inputField.SetValue(self.calculation)
def left(self, event):
self.calculation = self.calculation + "("
self.inputField.SetValue(self.calculation)
def right(self, event):
self.calculation = self.calculation + ")"
self.inputField.SetValue(self.calculation)
def backspace(self,event):
self.calculation = self.calculation[0:-1:1]
self.inputField.SetValue(self.calculation)
def equal(self, event):
try:
result = eval(self.calculation) #计算式求值
self.inputField.Insert(self.calculation, 0) #将历史记录加入下拉框
self.inputField.SetValue(str(result)) #输出框值为结果
except Exception as e:
self.inputField.SetValue('Input Illegal')
print(e)
return
finally:
self.calcuation = ""
if name == 'main':
app = wx.App()
frame = Calculator(parent=None, id=-1)
frame.Show()
app.MainLoop()