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 读取用户指令和格式化打印实现解析

Python 读取用户指令和格式化打印实现解析

一、读取用户指令 当你的程序要接收用户输入的指令时,可以用input函数: name = input("请输入你的名字:") print("Hi " + name) 程序中只要...

一个基于flask的web应用诞生 组织结构调整(7)

一个基于flask的web应用诞生 组织结构调整(7)

现在所有的Py代码均写在default.py文件中,很明显这种方法下,一旦程序变的负责,那么无论对于开发和维护来说,都会带来很多问题。 Flask框架并不强制要求项目使用特定的组织结构,...

Django通过dwebsocket实现websocket的例子

与django推荐的channel不同,dwebsocket使用更加方便简单 使用方法1: 只需views.py文件中,将对应的视图函数添加装饰器 accept_websocket-...

利用scrapy将爬到的数据保存到mysql(防止重复)

利用scrapy将爬到的数据保存到mysql(防止重复)

前言 本文主要给大家介绍了关于scrapy爬到的数据保存到mysql(防止重复)的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。 1.环境建立  ...

通过python实现windows桌面截图代码实例

这篇文章主要介绍了python实现windows桌面截图代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 代码实例 impo...