如何使用Python实现计算器功能

  介绍

这篇文章将为大家详细讲解有关如何使用Python实现计算器功能,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

源码:

#, - *安康;编码:utf-8  - * -   # ! python2   得到tkinter  import  *   时间=__author__  & # 39; tianshl& # 39;   时间=__date__  & # 39; 2017/10/16& # 39;   class 应用程序(帧):   自我,def  __init__ ():   Frame.__init__才能(自我)   self.grid才能()   时间=self.mem 才能;& # 39;& # 39;,,,,#,内存中的数据   时间=self.opt 才能;& # 39;& # 39;,,,,#,操作符   时间=self.display 才能;StringVar(), #,显示的数据   self.display.set才能(& # 39;0 & # 39;),,#,初始值   时间=self.need_cls 才能;False , #,是否需要清屏   self.create_widgets才能()   ,#清空   ,def 明确的(自我):   时间=self.mem 才能;& # 39;& # 39;   self.display.set才能(& # 39;0 & # 39;)   ,#取反   ,def 消极的(自我):   self.display.set才能(eval (& # 39; & # 39;, +, self.display.get ()))   ,#四则运算   ,def 选项(自我,,选择):   if 才能;not  self.need_cls:   ,,self.calculate ()   self.opt =,才能选择   self.need_cls 才能=,真的   时间=self.mem 才能;self.display.get ()   ,#计算结果   ,def 计算(自我):   if 才能;self.opt:   ,,试一试:   ,,,self.display.set (eval(时间+ self.mem  self.opt  +, self.display.get ()))   ,,except 例外:   ,,,self.display.set(& # 39;错误& # 39;)   ,,,self.need_cls =,真的   ,,self.opt =, & # 39; & # 39;   ,,self.mem =, & # 39; & # 39;   ,#百分比   ,def 百分比(自我):   base 才能=,浮子(1)self.mem ,或是,/,100年   display 才能=,eval (& # 39; {} * {} & # 39; .format (self.display.get(),基地))   int_display 才能=,int(显示)   时间=display 才能;int_display  if  display ==, int_display  else 显示   self.display.set才能(显示)   self.need_cls 才能=,真的   ,#输入   ,def 输入(自我,,键):   if 才能;self.need_cls:   ,,self.display.set (& # 39; 0 & # 39;)   ,,self.need_cls =False   时间=display 才能;self.display.get ()   if 才能;display ==, & # 39; 0 & # 39;,以及key  !=, & # 39; # 39;公司:   ,,self.display.set(关键)   其他的才能:   ,,if  & # 39;强生# 39;,拷贝display 以及key ==, & # 39; # 39;公司:   ,才能返回   ,,self.display.set(时间+ display 键)   ,#创建组件   ,def  create_widgets(自我):   #,才能显示框   ,,条目(自我,textvariable=self.display,状态=皉eadonly",,宽度=35).grid (=0,,,行,列=0,,columnspan=4)   #,才能键盘   keyboards 才能=,(   ,,(& # 39;c # 39;,, & # 39; +/& # 39;,, & # 39; % & # 39;,, & # 39;/& # 39;),,   ,,(& # 39;7 & # 39;,,& # 39;8 & # 39;,,& # 39;9 & # 39;,,& # 39;* & # 39;),,   ,,(& # 39;4 & # 39;,,& # 39;5 & # 39;,,& # 39;6 & # 39;,,& # 39;& # 39;],   ,,(& # 39;1 & # 39;,,& # 39;2 & # 39;,,& # 39;3 & # 39;,,& # 39;+ & # 39;],   ,,(& # 39;0 & # 39;,,& # 39;强生# 39;,,& # 39;=& # 39;】   ,,)   for 才能行,keys 拷贝列举(键盘):   ,,row_num =, 3 +,行   ,,for 坳,key 拷贝列举(键):   ,,,if  key ==, & # 39; c # 39;:   ,,,,command =self.clear   ,,,elif  key ==, & # 39; +/& # 39;:   ,,,,command =self.negative   ,,,elif  key ==, & # 39; % & # 39;   ,,,,command =self.percent   ,,,elif  key 拷贝(& # 39;+ & # 39;,,& # 39;& # 39;,,& # 39;* & # 39;,,& # 39;/& # 39;):   ,,,,command =, lambda  s=关键:,self.option (s)   ,,,elif  key ==, & # 39;=& # 39;:   ,,,,command =self.calculate   ,,,:   ,,,,command =, lambda  s=关键:,self.input (s)   ,,,bt =,按钮(自我,,文本=键,,命令=命令,,宽度=6)   ,,,bt.grid (=row_num,行,列=坳)   时间=app 应用程序()   #,设置窗口标题:   app.master.title (& # 39; www.jb51.net ,安康,计算器& # 39;)   #,设置窗口尺寸/位置   app.master.geometry (“326 x170 + 200 + 200“)   #,设置窗口不可变   app.master.resizable(宽度=False,身高=False)   #,主消息循环:   null

如何使用Python实现计算器功能