RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
python实现简易版计算器-创新互联

学了一周的Python,这篇文章算是为这段时间自学做的小总结。

创新互联长期为1000+客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为怀仁企业提供专业的成都做网站、成都网站制作,怀仁网站改版等技术服务。拥有十年丰富建站经验和众多成功案例,为您定制开发。

一、Python简介

       Python是一门十分优美的脚本语言,如果学过java、c++那入门Python是非常简单的。Python具有丰富和强大的类库。它常被昵称为胶水语言,它能够很轻松的把用其他语言制作的各种模块(尤其是C/C++)轻松地联结在一起。常见的一种应用情形是,使用Python快速生成程序的原型(有时甚至是程序的最终界面),然后对其中有特别要求的部分,用更合适的语言改写。

二、Python版计算器的实现

工具准备:

     1. Python2.7
        2. wxPython
        3. wxGlade界面编辑工具
        4. pyCharm

        具体配置什么的我就不说了,网上有很多。Python的GUI开发库有很多,这里我们使用wxPython。用wxGlade编辑界面,wxGlade是一款很好用的GUI编辑工具,它大的优点是能自动生成编辑好的图形界面的代码。

这里主要介绍几点:

wx.TextCtrl(文本输入控件),这个作为计算器的显示屏,点击数字时用AppendText方法将数字添加至文本末尾,要取出数字时用GetValue方法,点击运算符时要将文本框中的数字清空(先存储)用Clear方法然后还有将操作符保存,点击等号后要将第二个操作数存储,然后用int方法将字符串转换为数字,计算后用SetValue方法将结果送入文本框,具体代码如下:

import wx 
 
# begin wxGlade: extracode 
# end wxGlade 
 
 
class MyFrame(wx.Frame): 
 def __init__(self, *args, **kwds): 
 # begin wxGlade: MyFrame.__init__ 
 kwds["style"] = wx.DEFAULT_FRAME_STYLE 
 wx.Frame.__init__(self, *args, **kwds) 
 self.text_ctrl_1 = wx.TextCtrl(self, -1, "",style=wx.TE_READONLY) 
 self.button_37 = wx.Button(self, -1, "1") 
 self.button_38 = wx.Button(self, -1, "2") 
 self.button_39 = wx.Button(self, -1, "3") 
 self.button_40 = wx.Button(self, -1, "+") 
 self.button_41 = wx.Button(self, -1, "4") 
 self.button_42 = wx.Button(self, -1, "5") 
 self.button_43 = wx.Button(self, -1, "6") 
 self.button_44 = wx.Button(self, -1, "-") 
 self.button_46 = wx.Button(self, -1, "7") 
 self.button_45 = wx.Button(self, -1, "8") 
 self.button_47 = wx.Button(self, -1, "9") 
 self.button_48 = wx.Button(self, -1, "x") 
 self.button_49 = wx.Button(self, -1, "C") 
 self.button_50 = wx.Button(self, -1, "0") 
 self.button_51 = wx.Button(self, -1, "=") 
 self.button_52 = wx.Button(self, -1, "/") 
 
 self.__set_properties() 
 self.__do_layout() 
 
 self.Bind(wx.EVT_BUTTON, self.bu1, self.button_37) 
 self.Bind(wx.EVT_BUTTON, self.bu2, self.button_38) 
 self.Bind(wx.EVT_BUTTON, self.bu3, self.button_39) 
 self.Bind(wx.EVT_BUTTON, self.bu_plus, self.button_40) 
 self.Bind(wx.EVT_BUTTON, self.bu4, self.button_41) 
 self.Bind(wx.EVT_BUTTON, self.bu5, self.button_42) 
 self.Bind(wx.EVT_BUTTON, self.bu6, self.button_43) 
 self.Bind(wx.EVT_BUTTON, self.bu_min, self.button_44) 
 self.Bind(wx.EVT_BUTTON, self.bu7, self.button_46) 
 self.Bind(wx.EVT_BUTTON, self.bu8, self.button_45) 
 self.Bind(wx.EVT_BUTTON, self.bu9, self.button_47) 
 self.Bind(wx.EVT_BUTTON, self.bu_mul, self.button_48) 
 self.Bind(wx.EVT_BUTTON, self.bu_clear, self.button_49) 
 self.Bind(wx.EVT_BUTTON, self.bu0, self.button_50) 
 self.Bind(wx.EVT_BUTTON, self.bu_result, self.button_51) 
 self.Bind(wx.EVT_BUTTON, self.bu_chu, self.button_52) 
 # end wxGlade 
 self.Show(True) 
 
 def __set_properties(self): 
 # begin wxGlade: MyFrame.__set_properties 
 self.SetTitle("Python Calculater by CYG") 
 self.text_ctrl_1.SetMinSize((400, 50)) 
 self.button_37.SetMinSize((100, 50)) 
 self.button_38.SetMinSize((100, 50)) 
 self.button_39.SetMinSize((100, 50)) 
 self.button_40.SetMinSize((100, 50)) 
 self.button_41.SetMinSize((100, 50)) 
 self.button_42.SetMinSize((100, 50)) 
 self.button_43.SetMinSize((100, 50)) 
 self.button_44.SetMinSize((100, 50)) 
 self.button_46.SetMinSize((100, 50)) 
 self.button_45.SetMinSize((100, 50)) 
 self.button_47.SetMinSize((100, 50)) 
 self.button_48.SetMinSize((100, 50)) 
 self.button_49.SetMinSize((100, 50)) 
 self.button_50.SetMinSize((100, 50)) 
 self.button_51.SetMinSize((100, 50)) 
 self.button_52.SetMinSize((100, 50)) 
 # end wxGlade 
 
 def __do_layout(self): 
 # begin wxGlade: MyFrame.__do_layout 
 sizer_2 = wx.BoxSizer(wx.VERTICAL) 
 sizer_3 = wx.BoxSizer(wx.VERTICAL) 
 grid_sizer_1 = wx.GridSizer(4, 4, 0, 0) 
 sizer_3.Add(self.text_ctrl_1, 0, 0, 0) 
 grid_sizer_1.Add(self.button_37, 0, 0, 0) 
 grid_sizer_1.Add(self.button_38, 0, 0, 0) 
 grid_sizer_1.Add(self.button_39, 0, 0, 0) 
 grid_sizer_1.Add(self.button_40, 0, 0, 0) 
 grid_sizer_1.Add(self.button_41, 0, 0, 0) 
 grid_sizer_1.Add(self.button_42, 0, 0, 0) 
 grid_sizer_1.Add(self.button_43, 0, 0, 0) 
 grid_sizer_1.Add(self.button_44, 0, 0, 0) 
 grid_sizer_1.Add(self.button_46, 0, 0, 0) 
 grid_sizer_1.Add(self.button_45, 0, 0, 0) 
 grid_sizer_1.Add(self.button_47, 0, 0, 0) 
 grid_sizer_1.Add(self.button_48, 0, 0, 0) 
 grid_sizer_1.Add(self.button_49, 0, 0, 0) 
 grid_sizer_1.Add(self.button_50, 0, 0, 0) 
 grid_sizer_1.Add(self.button_51, 0, 0, 0) 
 grid_sizer_1.Add(self.button_52, 0, 0, 0) 
 sizer_3.Add(grid_sizer_1, 1, wx.EXPAND, 0) 
 sizer_2.Add(sizer_3, 1, wx.EXPAND, 0) 
 self.SetSizer(sizer_2) 
 sizer_2.Fit(self) 
 self.Layout() 
 # end wxGlade 
 
 def bu1(self, event): # wxGlade: MyFrame. 
 self.text_ctrl_1.AppendText("1") 
 event.Skip() 
 
 def bu2(self, event): # wxGlade: MyFrame. 
 self.text_ctrl_1.AppendText("2") 
 event.Skip() 
 
 def bu3(self, event): # wxGlade: MyFrame. 
 self.text_ctrl_1.AppendText("3") 
 event.Skip() 
 
 def bu_plus(self, event): # wxGlade: MyFrame. 
 self.num1=self.text_ctrl_1.GetValue() 
 self.op="+" 
 self.text_ctrl_1.Clear() 
 event.Skip() 
 
 def bu4(self, event): # wxGlade: MyFrame. 
 self.text_ctrl_1.AppendText("4") 
 event.Skip() 
 
 def bu5(self, event): # wxGlade: MyFrame. 
 self.text_ctrl_1.AppendText("5") 
 event.Skip() 
 
 def bu6(self, event): # wxGlade: MyFrame. 
 self.text_ctrl_1.AppendText("6") 
 event.Skip() 
 
 def bu_min(self, event): # wxGlade: MyFrame. 
 self.num1=self.text_ctrl_1.GetValue() 
 self.op="-" 
 self.text_ctrl_1.Clear() 
 event.Skip() 
 
 def bu7(self, event): # wxGlade: MyFrame. 
 self.text_ctrl_1.AppendText("7") 
 event.Skip() 
 
 def bu8(self, event): # wxGlade: MyFrame. 
 self.text_ctrl_1.AppendText("8") 
 event.Skip() 
 
 def bu9(self, event): # wxGlade: MyFrame. 
 self.text_ctrl_1.AppendText("9") 
 event.Skip() 
 
 def bu_mul(self, event): # wxGlade: MyFrame. 
 self.num1=self.text_ctrl_1.GetValue() 
 self.op="x" 
 self.text_ctrl_1.Clear() 
 event.Skip() 
 
 def bu_clear(self, event): # wxGlade: MyFrame. 
 self.text_ctrl_1.Clear() 
 event.Skip() 
 
 def bu0(self, event): # wxGlade: MyFrame. 
 self.text_ctrl_1.AppendText("0") 
 event.Skip() 
 
 def bu_chu(self, event): # wxGlade: MyFrame. 
 self.num1=self.text_ctrl_1.GetValue() 
 self.op="/" 
 self.text_ctrl_1.Clear() 
 event.Skip() 
 
 def bu_result(self, event): # wxGlade: MyFrame. 
 self.num2=self.text_ctrl_1.GetValue() 
 num1=int(self.num1) 
 num2=int(self.num2) 
 if self.op=="+": 
  self.text_ctrl_1.SetValue(str(num1+num2)) 
 elif self.op=="-": 
  self.text_ctrl_1.SetValue(str(num1-num2)) 
 elif self.op=="x": 
  self.text_ctrl_1.SetValue(str(num1*num2)) 
 elif self.op=="/": 
  self.text_ctrl_1.SetValue(str(num1/num2)) 
 event.Skip() 
 
 
# end of class MyFrame 
if __name__ == "__main__": 
 
 app=wx.App(False) 
 myframe=MyFrame(None) 
 app.MainLoop() 

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文名称:python实现简易版计算器-创新互联
地址分享:http://lswzjz.com/article/cecsip.html