wxPython中文教程入门实例

2019-10-06 14:28:52刘景俊

return 
self.display.AppendText('+') 
def OnDot(self, event): 
if self.formula: 
return 
self.display.AppendText('.') 
def OnEqual(self, event): 
if self.formula: 
return 
formula = self.display.GetValue() 
self.formula = True 
try: 
self.display.Clear() 
output = eval(formula) 
self.display.AppendText(str(output)) 
except StandardError: 
self.display.AppendText("Error") 
def OnZero(self, event): 
if self.formula: 
self.display.Clear() 
self.formula = False 
self.display.AppendText('0') 
def OnOne(self, event): 
if self.formula: 
self.display.Clear() 
self.formula = False 
self.display.AppendText('1') 
def OnTwo(self, event): 
if self.formula: 
self.display.Clear() 
self.formula = False 
self.display.AppendText('2') 
def OnThree(self, event): 
if self.formula: 
self.display.Clear() 
self.formula = False 
self.display.AppendText('3') 
def OnFour(self, event): 
if self.formula: 
self.display.Clear() 
self.formula = False 
self.display.AppendText('4') 
def OnFive(self, event): 
if self.formula: 
self.display.Clear() 
self.formula = False 
self.display.AppendText('5') 
def OnSix(self, event): 
if self.formula: 
self.display.Clear() 
self.formula = False 
self.display.AppendText('6') 
def OnSeven(self, event): 
if self.formula: 
self.display.Clear() 
self.formula = False 
self.display.AppendText('7') 
def OnEight(self, event): 
if self.formula: 
self.display.Clear() 
self.formula = False 
self.display.AppendText('8') 
def OnNine(self, event): 
if self.formula: 
self.display.Clear() 
self.formula = False 
self.display.AppendText('9') 

class MyApp(wx.App): 
def OnInit(self): 
frame = MyFrame(None, -1, "calculator.py") 
frame.Show(True) 
self.SetTopWindow(frame) 
return True 
app = MyApp(0) 
app.MainLoop()

输入的公式使用 python 的内置函数 eval 来处理。
output = eval( formula )

如果公式有错,就会显示一条错误信息。
请注意如何在 Bck 和 Close 按纽之间插入空白的。
只是简单的在那放了一个空的 wx.StaticText。这是一个很常用的技巧。