python使用tkinter实现简单计算器

yipeiwu_com5年前Python基础

本文实例为大家分享了python使用tkinter实现简单计算器的具体代码,供大家参考,具体内容如下

class Counter: 
 #引入tkinter 
 import tkinter as tk 
 #引入消息弹窗模块 
 import tkinter.messagebox as mbox 
 
 #初始化Counter 
 def __init__(self): 
 #生成一个窗口对象 
 self.window = self.tk.Tk() 
 #命名窗口对象的显示title 
 self.window.title('计算器') 
 #设置窗口的大小 
 self.window.minsize(240, 325) 
 self.window.maxsize(240, 325) 
 #是否清空显示框判定参数 
 self.is_init_lable = False 
 #设置菜单 
 self.set_menu() 
 #设置显示框 
 self.lable_show = self.tk.Label(text='', anchor='se', font=('黑体', 30), fg='black') 
 self.lable_show.place(x=0, y=0, width=240, height=80) 
 #设置按钮组件 
 self.set_buttons() 
 #将窗口放入主消息队列 
 self.window.mainloop() 
 
 #设置菜单 
 def set_menu(self): 
 #创建总菜单 
 menubar = self.tk.Menu(self.window) 
 #创建一个下拉菜单,并且加入文件菜单 
 filemenu = self.tk.Menu(menubar, tearoff=0) 
 #创建下来菜单的选项 
 filemenu.add_command(label="退出计算器", command=self.window.quit) 
 #print author的函数 
 def show_author(): 
  self.mbox.showinfo(message='Wiz333@XDL 2017') 
 filemenu.add_command(label="作者", command=show_author) 
 #将文件菜单作为下拉菜单添加到总菜单中,并且将命名为操作 
 menubar.add_cascade(label="操作", menu=filemenu) 
 #显示总菜单 
 self.window.config(menu=menubar) 
 
 #设置按钮组件 
 def set_buttons(self): 
 #7 
 btn7 = self.tk.Button(text='7', bd=2, font='黑体') 
 btn7.place(x=0, y=90, width=60, height=40) 
 #8 
 btn8 = self.tk.Button(text='8', bd=2, font='黑体') 
 btn8.place(x=60, y=90, width=60, height=40) 
 #9 
 btn9 = self.tk.Button(text='9', bd=2, font='黑体') 
 btn9.place(x=120, y=90, width=60, height=40) 
 #+ 
 btn_jia = self.tk.Button(text='+', bd=2, font='黑体') 
 btn_jia.place(x=180, y=90, width=60, height=40) 
 #4 
 btn4 = self.tk.Button(text='4', bd=2, font='黑体') 
 btn4.place(x=0, y=130, width=60, height=40) 
 #5 
 btn5 = self.tk.Button(text='5', bd=2, font='黑体') 
 btn5.place(x=60, y=130, width=60, height=40) 
 #6 
 btn6 = self.tk.Button(text='6', bd=2, font='黑体') 
 btn6.place(x=120, y=130, width=60, height=40) 
 #- 
 btn_jian = self.tk.Button(text='-', bd=2, font='黑体') 
 btn_jian.place(x=180, y=130, width=60, height=40) 
 #1 
 btn1 = self.tk.Button(text='1', bd=2, font='黑体') 
 btn1.place(x=0, y=170, width=60, height=40) 
 #2 
 btn2 = self.tk.Button(text='2', bd=2, font='黑体') 
 btn2.place(x=60, y=170, width=60, height=40) 
 #3 
 btn3 = self.tk.Button(text='3', bd=2, font='黑体') 
 btn3.place(x=120, y=170, width=60, height=40) 
 #* 
 btn_cheng = self.tk.Button(text='*', bd=2, font='黑体') 
 btn_cheng.place(x=180, y=170, width=60, height=40) 
 #0 
 btn0 = self.tk.Button(text='0', bd=2, font='黑体') 
 btn0.place(x=0, y=210, width=120, height=40) 
 #. 
 btn_point = self.tk.Button(text='.', bd=2, font='黑体') 
 btn_point.place(x=120, y=210, width=60, height=40) 
 #/ 
 btn_chu = self.tk.Button(text='/', bd=2, font='黑体') 
 btn_chu.place(x=180, y=210, width=60, height=40) 
 #取消 
 btn_cancel = self.tk.Button(text='C', bd=2, font='黑体') 
 btn_cancel.place(x=0, y=250, width=60, height=40) 
 #确定 
 btn_ok = self.tk.Button(text='=', bd=2, font='黑体') 
 btn_ok.place(x=60, y=250, width=180, height=40) 
 #绑定Button的点击事件 
 btn7.bind_class('Button', '<Button-1>', self.click_button) 
 
 #绑定Button的点击事件 
 def click_button(self,e): 
 #判断是否是新的运算,如果是则清空显示框 
 if self.is_init_lable: 
  self.lable_show['text'] = '' 
  self.is_init_lable = False 
 #label_show显示的累加 
 font = e.widget['text'] 
 self.lable_show['text'] += font 
 #异常捕获 
 try: 
  #判定运算符号重复的时候,使用最后输入的符号 
  if self.lable_show['text'][-1] in ['+','-','*','/'] and self.lable_show['text'][-2] in ['+','-','*','/']: 
  header = self.lable_show['text'][:-2] 
  footer = self.lable_show['text'][-1] 
  self.lable_show['text'] = header+footer 
 except: 
  pass 
 
 #普通计算 
 if e.widget['text'] == '=': 
  try: 
  res = eval(self.lable_show['text'][:-1]) 
  #print(res) 
  #小数点取到9位 
  self.lable_show['text'] = str(round(float(res), 5)) 
  self.isinit = True 
  except ZeroDivisionError: 
  #除法时,除数不能为0 
  self.mbox.showerror(message='除法计算时!除数不能为0!') 
  except: 
  self.mbox.showerror(message='算式有误') 
 #取消当前输入的字符 
 if e.widget['text'] == 'C': 
  cancel_res = self.lable_show['text'][:-2] 
  self.lable_show['text'] = cancel_res 
 
 
#实例化计算器对象 
wiz = Counter()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python产生Gnuplot绘图数据的方法

Python产生Gnuplot绘图数据的方法

gnuplot的绘图可以直接选取文件绘图,文件格式要求如下: x1 y1 x2 y2 ...... xn yn 在python中利用文件操作的write方法可以非常方便实现,在此记录一...

详解python读取和输出到txt

详解python读取和输出到txt

读取txt的数据和把数据保存到txt中是经常要用到的,下面我就总结一下。 读txt文件 python常用的读取文件函数有三种read()、readline()、readlines()...

python登录WeChat 实现自动回复实例详解

python登录WeChat 实现自动回复实例详解

最近实现了一些微信的简单玩法 我们可以通过网页版的微信微信网页版,扫码登录后去抓包爬取信息,还可以post去发送信息。 》》安装itchat这个库  &nb...

Python使用pygame模块编写俄罗斯方块游戏的代码实例

Python使用pygame模块编写俄罗斯方块游戏的代码实例

文章先介绍了关于俄罗斯方块游戏的几个术语。 边框——由10*20个空格组成,方块就落在这里面。 盒子——组成方块的其中小方块,是组成方块的基本单元。 方块——从边框顶掉下的...

Python3使用requests发闪存的方法

requests是一个python 轻量的http客户端库,相比python的标准库要优雅很多。接下来通过本文给大家介绍Python3使用requests发闪存的方法,一起学习吧。 使...