通过视频学习,学习了python中怎么实现一个计算器,主要运用了wxPython 库。原视频中是python2.7版本,我把它修改成了python3.6版本
学习视频链接如下:计算器
wxPython库安装链接如下:wxPython各种版本的.whl和.zip文件
我安装的是python3.6版本win64位的.whl
wxPython-4.0.0b2-cp36-cp36m-win_amd64.whl
代码如下:
import wx
class CalcFrame(wx.Frame):
def __init__(self, title):
wx.Frame.__init__(self, None, -1, title, size=(300, 320),
style=wx.DEFAULT_FRAME_STYLE ^ (wx.MINIMIZE_BOX | wx.MAXIMIZE_BOX))
# 建立面板
panel = wx.Panel(self)
# 建sizer后,窗口可根据大小自动调整, wx.VERTICAL, wx.HORIZONTAL
boxsize = wx.BoxSizer(wx.VERTICAL)
# 建grid便于放置控件
gridBox = wx.GridSizer(rows=6, cols=5, hgap=1, vgap=1)
self.equation = ""
# 建计算器显示屏
self.textprint = wx.TextCtrl(panel, -1, '', style=wx.TE_RIGHT | wx.TE_READONLY)
self.bgFont = wx.Font(pointSize=25, family=wx.FONTFAMILY_SWISS,
style=wx.FONTSTYLE_NORMAL, weight=wx.FONTWEIGHT_NORMAL)
#在python2.7版本中 self.bgFont = wx.Font(25,wx.SWISS,wx.NORMAL,wx.BOLD,face=u"Roboto")
self.textprint.SetFont(self.bgFont)
self.textprint.SetBackgroundColour((210, 210, 210))
self.textprint.SetForegroundColour((15, 15, 15))
# 按钮数据
self.buttonData="7 8 9 DEL AC 4 5 6 * ÷ 1 2 3 + - 0 % pi e sqrt ^ sin cos tan log10 ln ( ) . =".split()
buttonLength = len(self.buttonData)
for i in range(buttonLength):
labels = "%s" % self.buttonData[i]
buttonIterm = wx.Button(panel, i, labels)
self.createHandler(buttonIterm, labels)
gridBox.Add(buttonIterm, 0, flag=wx.EXPAND)
boxsize.Add(self.textprint, 1, flag=wx.EXPAND)
boxsize.Add(gridBox, 5, flag=wx.EXPAND)
panel.SetSizerAndFit(boxsize)
# 创建按钮处理方法
def createHandler(self, button, labels):
item = "DEL AC ="
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)
# 添加运算符与数字
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
if '^' in string:
string = string.replace('^', '**')
if 'ln' in string:
string = string.replace('ln', 'log')
try:
target = eval(string)
self.equation = str(target)
self.textprint.SetValue(str(target))
except SyntaxError:
dlg = wx.MessageDialog(self, u'格式错误,请输入正确的等式!',
u'请注意', wx.OK | wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
class App(wx.App):
def OnInit(self):
self.frame = CalcFrame(u'计算器')
self.frame.Center()
self.frame.Show()
return True
if __name__ == '__main__':
app = App()
app.MainLoop()