project1_calculator(使用tkinter实现python计算器,含有具体过程与注释)
最终的运行效果图(程序见序号7):
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# ------------------------------------------------------------
#
# 参考资料:
# tkinter事件之bind - 鹄望 - 博客园
# https://www.cnblogs.com/huwang-sun/p/7064048.html
#
# Python 3 Tkinter教程之事件Event绑定处理代码实例_python_编程语言_169IT.COM
# http://www.169it.com/article/11243858854023511493.html
#
# python Tkinter之Button - 一杯明月 - 博客园
# https://www.cnblogs.com/yibeimingyue/p/9395219.html
#
#
# 80行代码使用Python+tkinter实现一个计算器 - CSDN博客
# https://blog.csdn.net/oh5W6HinUg43JvRhhB/article/details/78804200
#
# python 3.+版 计算器 - CSDN博客
# https://blog.csdn.net/xueshao110/article/details/78973416
#
# Python(16)python使用tkinter实现一个简单的计算器 - CSDN博客
# https://blog.csdn.net/wizblack/article/details/78932258
#
# ------------------------------------------------------------
# ******************** project1_calculator *******************
# ******************** project1_calculator *******************
# =====>>>>>>内容概览
# =====>>>>>>内容概览
'''
# ------------------------------------------------------------
# # 1、窗口以及相关的菜单栏
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 2、建立按钮
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 3、设置显示框, 将输入绑定到显示框
# # # 增加位置: def __init__(self):; def click_button(self, event):
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 4、实现基本的计算器功能
# # # 改动位置位置: def click_button(self, event):
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 5、实现基本的计算器功能,补充错误检测
# # # 改动位置位置: def click_button(self, event):
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 6、实现基本的计算器功能,补充错误检测,补充‘C’后退输入功能
# # # 改动位置位置: def click_button(self, event):
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 7、(完成全部设计)实现基本的计算器功能,补充错误检测, 修改‘C’为清楚功能, '<=='为后退功能
# # # 改动位置位置: def click_button(self, event):
# ------------------------------------------------------------
'''
# ------------------------------------------------分割线-------------------------------------------------
# ------------------------------------------------分割线-------------------------------------------------
# ------------------------------------------------分割线-------------------------------------------------
'''
# ------------------------------------------------------------
# # 1、窗口以及相关的菜单栏
# ------------------------------------------------------------
'''
class Calculator:
import tkinter as tk
import tkinter.messagebox as mbox
def __init__(self):
# 生成一个窗口对象
self.window = self.tk.Tk()
# 命名窗口对象的显示title
self.window.title('计算器')
# 设置窗口的大小 minsize最小 maxsize最大
self.window.minsize(240,325)
self.window.maxsize(240, 325)
# 设置菜单
self.set_menu()
# 将窗口放入主消息队列
self.window.mainloop()
def set_menu(self):
'''
设置菜单
:return: None
'''
# 创建总菜单
menubar = self.tk.Menu(self.window)
# 创建一个下拉菜单,并且加入文件菜单
filemenu = self.tk.Menu(menubar, tearoff=False)
# 创建菜单中的选项
filemenu.add_command(label='退出计算器', command=self.window.quit )
# print author的函数
def show_author():
self.mbox.showinfo(title='作者信息',message='作者:许建JY小脚丫\n联系邮箱:******print@foxmail.com')
filemenu.add_command(label='作者信息', command=show_author)
# 将文件菜单作为下拉菜单添加到总菜单中,并且将命名为操作
menubar.add_cascade(label='查看', menu=filemenu)
# 显示总菜单
self.window.config(menu=menubar)
start = Calculator()
运行效果图1:
# ------------------------------------------------分割线-------------------------------------------------
'''
# ------------------------------------------------------------
# # 2、建立按钮
# ------------------------------------------------------------
'''
class Calculator:
import tkinter as tk
import tkinter.messagebox as mbox
def __init__(self):
# 生成一个窗口对象
self.window = self.tk.Tk()
# 命名窗口对象的显示title
self.window.title('计算器')
# 设置窗口的大小 minsize最小 maxsize最大
self.window.minsize(240,325)
self.window.maxsize(240, 325)
# 是否清空显示框判定参数
# ????
# 设置菜单
self.set_menu()
# 设置按钮组件
self.set_buttons()
# 将窗口放入主消息队列
self.window.mainloop()
def set_menu(self):
'''
设置菜单
:return: None
'''
# 创建总菜单
menubar = self.tk.Menu(self.window)
# 创建一个下拉菜单,并且加入文件菜单
filemenu = self.tk.Menu(menubar, tearoff=False)
# 创建菜单中的选项
filemenu.add_command(label='退出计算器', command=self.window.quit )
# print author的函数
def show_author():
self.mbox.showinfo(title='作者信息',message='作者:JY小脚丫\n联系邮箱:******print@foxmail.com')
filemenu.add_command(label='作者信息', command=show_author)
# 将文件菜单作为下拉菜单添加到总菜单中,并且将命名为操作
menubar.add_cascade(label='查看', menu=filemenu)
# 显示总菜单
self.window.config(menu=menubar)
def set_buttons(self):
# 基础坐标, x0, y0,开始坐标; x_width, y_width 间隔
x0, y0, x_width, y_width,height = 0, 90, 60, 40, 40
# 7
btn7 = self.tk.Button(text='7', bd=2, font='黑体')
btn7.place( x=x0, y=y0, width=x_width, height=y_width)
# 8
btn8 = self.tk.Button(text='8', bd=2, font='黑体')
btn8.place( x=x0+x_width*1, y=y0, width=x_width, height=y_width)
# 9
btn9 = self.tk.Button(text='9', bd=2, font='黑体')
btn9.place( x=x0+x_width*2, y=y0, width=x_width, height=y_width)
# +
btn_add = self.tk.Button(text='+', bd=2, font='黑体')
btn_add.place( x=x0+x_width*3, y=y0, width=x_width, height=y_width)
# 4
btn4 = self.tk.Button(text='4', bd=2, font='黑体')
btn4.place( x=x0, y=y0+y_width, width=x_width, height=y_width)
# 5
btn5 = self.tk.Button(text='5', bd=2, font='黑体')
btn5.place( x=x0+x_width*1, y=y0+y_width, width=x_width, height=y_width)
# 6
btn6 = self.tk.Button(text='6', bd=2, font='黑体')
btn6.place( x=x0+x_width*2, y=y0+y_width, width=x_width, height=y_width)
# -
btn_subtract = self.tk.Button(text='-', bd=2, font='黑体')
btn_subtract.place( x=x0+x_width*3, y=y0+y_width, width=x_width, height=y_width)
# 1
btn1 = self.tk.Button(text='1', bd=2, font='黑体')
btn1.place( x=x0, y=y0+y_width*2, width=x_width, height=y_width)
# 2
btn2 = self.tk.Button(text='2', bd=2, font='黑体')
btn2.place( x=x0+x_width*1, y=y0+y_width*2, width=x_width, height=y_width)
# 3
btn3 = self.tk.Button(text='3', bd=2, font='黑体')
btn3.place( x=x0+x_width*2, y=y0+y_width*2, width=x_width, height=y_width)
# *
btn_mutiply = self.tk.Button(text='*', bd=2, font='黑体')
btn_mutiply.place( x=x0+x_width*3, y=y0+y_width*2, width=x_width, height=y_width)
# 0
btn0 = self.tk.Button(text='0', bd=2, font='黑体')
btn0.place( x=x0, y=y0+y_width*3, width=x_width*2, height=y_width)
# .
btn_point = self.tk.Button(text='.', bd=2, font='黑体')
btn_point.place( x=x0+x_width*2, y=y0+y_width*3, width=x_width, height=y_width)
# /
btn_divid = self.tk.Button(text='/', bd=2, font='黑体')
btn_divid.place( x=x0+x_width*3, y=y0+y_width*3, width=x_width, height=y_width)
# C后退
btn_back = self.tk.Button( text='C', bd=2, font='黑体')
btn_back.place( x=x0, y=y0+y_width*4, width=x_width*1, height=y_width)
# =
btn_equal = self.tk.Button( text='=', bd=2, font='黑体')
btn_equal.place( x=x0+x_width*1, y=y0+y_width*4, width=x_width*3, height=y_width)
start = Calculator()
运行效果图2:
# ------------------------------------------------分割线-------------------------------------------------
'''
# ------------------------------------------------------------
# # 3、设置显示框, 将输入绑定到显示框
# # # 增加位置: def __init__(self):; def click_button(self, event):
# ------------------------------------------------------------
'''
class Calculator:
import tkinter as tk
import tkinter.messagebox as mbox
def __init__(self):
# 生成一个窗口对象
self.window = self.tk.Tk()
# 命名窗口对象的显示title
self.window.title('计算器')
# 设置窗口的大小 minsize最小 maxsize最大
self.window.minsize(240, 290)
self.window.maxsize(240, 290)
# 是否清空显示框判定参数
# ????
# 设置菜单
self.set_menu()
# 设置显示框
self.label_show = self.tk.Label(text='', anchor='se', font=('黑体', 30), fg='black')
self.label_show.place(x=0, y=0, width=240,height=80)
# 设置按钮组件
self.set_buttons()
# 将窗口放入主消息队列
self.window.mainloop()
def set_menu(self):
'''
设置菜单
:return: None
'''
# 创建总菜单
menubar = self.tk.Menu(self.window)
# 创建一个下拉菜单,并且加入文件菜单
filemenu = self.tk.Menu(menubar, tearoff=False)
# 创建菜单中的选项
filemenu.add_command(label='退出计算器', command=self.window.quit )
# print author的函数
def show_author():
self.mbox.showinfo(title='作者信息',message='作者:JY小脚丫\n联系邮箱:******print@foxmail.com')
filemenu.add_command(label='作者信息', command=show_author)
# 将文件菜单作为下拉菜单添加到总菜单中,并且将命名为操作
menubar.add_cascade(label='查看', menu=filemenu)
# 显示总菜单
self.window.config(menu=menubar)
def set_buttons(self):
# 基础坐标, x0, y0,开始坐标; x_width, y_width 间隔
x0, y0, x_width, y_width,height = 0, 90, 60, 40, 40
# 7
btn7 = self.tk.Button(text='7', bd=2, font='黑体')
btn7.place( x=x0, y=y0, width=x_width, height=y_width)
# 8
btn8 = self.tk.Button(text='8', bd=2, font='黑体')
btn8.place( x=x0+x_width*1, y=y0, width=x_width, height=y_width)
# 9
btn9 = self.tk.Button(text='9', bd=2, font='黑体')
btn9.place( x=x0+x_width*2, y=y0, width=x_width, height=y_width)
# +
btn_add = self.tk.Button(text='+', bd=2, font='黑体')
btn_add.place( x=x0+x_width*3, y=y0, width=x_width, height=y_width)
# 4
btn4 = self.tk.Button(text='4', bd=2, font='黑体')
btn4.place( x=x0, y=y0+y_width, width=x_width, height=y_width)
# 5
btn5 = self.tk.Button(text='5', bd=2, font='黑体')
btn5.place( x=x0+x_width*1, y=y0+y_width, width=x_width, height=y_width)
# 6
btn6 = self.tk.Button(text='6', bd=2, font='黑体')
btn6.place( x=x0+x_width*2, y=y0+y_width, width=x_width, height=y_width)
# -
btn_subtract = self.tk.Button(text='-', bd=2, font='黑体')
btn_subtract.place( x=x0+x_width*3, y=y0+y_width, width=x_width, height=y_width)
# 1
btn1 = self.tk.Button(text='1', bd=2, font='黑体')
btn1.place( x=x0, y=y0+y_width*2, width=x_width, height=y_width)
# 2
btn2 = self.tk.Button(text='2', bd=2, font='黑体')
btn2.place( x=x0+x_width*1, y=y0+y_width*2, width=x_width, height=y_width)
# 3
btn3 = self.tk.Button(text='3', bd=2, font='黑体')
btn3.place( x=x0+x_width*2, y=y0+y_width*2, width=x_width, height=y_width)
# *
btn_mutiply = self.tk.Button(text='*', bd=2, font='黑体')
btn_mutiply.place( x=x0+x_width*3, y=y0+y_width*2, width=x_width, height=y_width)
# 0
btn0 = self.tk.Button(text='0', bd=2, font='黑体')
btn0.place( x=x0, y=y0+y_width*3, width=x_width*2, height=y_width)
# .
btn_point = self.tk.Button(text='.', bd=2, font='黑体')
btn_point.place( x=x0+x_width*2, y=y0+y_width*3, width=x_width, height=y_width)
# /
btn_divid = self.tk.Button(text='/', bd=2, font='黑体')
btn_divid.place( x=x0+x_width*3, y=y0+y_width*3, width=x_width, height=y_width)
# C后退
btn_back = self.tk.Button( text='C', bd=2, font='黑体')
btn_back.place( x=x0, y=y0+y_width*4, width=x_width*1, height=y_width)
# =
btn_equal = self.tk.Button( text='=', bd=2, font='黑体')
btn_equal.place( x=x0+x_width*1, y=y0+y_width*4, width=x_width*3, height=y_width)
# 绑定Button的点击事件
btn7.bind_class('Button', '<Button-1>', self.click_button)
def click_button(self, event):
# 输入的按键信息
input_event = event.widget['text']
# 输入的按键信息显示
self.label_show['text'] = self.label_show['text'] + input_event
start = Calculator()
运行效果图3:
# ------------------------------------------------分割线-------------------------------------------------
'''
# ------------------------------------------------------------
# # 4、实现基本的计算器功能
# # # 改动位置位置: def click_button(self, event):
# ------------------------------------------------------------
'''
class Calculator:
import tkinter as tk
import tkinter.messagebox as mbox
def __init__(self):
# 生成一个窗口对象
self.window = self.tk.Tk()
# 命名窗口对象的显示title
self.window.title('计算器')
# 设置窗口的大小 minsize最小 maxsize最大
self.window.minsize(240, 290)
self.window.maxsize(240, 290)
# 是否清空显示框判定参数
# ????
# 设置菜单
self.set_menu()
# 设置显示框
self.label_show = self.tk.Label(text='', anchor='se', font=('黑体', 30), fg='black')
self.label_show.place(x=0, y=0, width=240,height=80)
# 设置按钮组件
self.set_buttons()
# 将窗口放入主消息队列
self.window.mainloop()
def set_menu(self):
'''
设置菜单
:return: None
'''
# 创建总菜单
menubar = self.tk.Menu(self.window)
# 创建一个下拉菜单,并且加入文件菜单
filemenu = self.tk.Menu(menubar, tearoff=False)
# 创建菜单中的选项
filemenu.add_command(label='退出计算器', command=self.window.quit )
# print author的函数
def show_author():
self.mbox.showinfo(title='作者信息',message='作者:JY小脚丫\n联系邮箱:******print@foxmail.com')
filemenu.add_command(label='作者信息', command=show_author)
# 将文件菜单作为下拉菜单添加到总菜单中,并且将命名为操作
menubar.add_cascade(label='查看', menu=filemenu)
# 显示总菜单
self.window.config(menu=menubar)
def set_buttons(self):
# 基础坐标, x0, y0,开始坐标; x_width, y_width 间隔
x0, y0, x_width, y_width,height = 0, 90, 60, 40, 40
# 7
btn7 = self.tk.Button(text='7', bd=2, font='黑体')
btn7.place( x=x0, y=y0, width=x_width, height=y_width)
# 8
btn8 = self.tk.Button(text='8', bd=2, font='黑体')
btn8.place( x=x0+x_width*1, y=y0, width=x_width, height=y_width)
# 9
btn9 = self.tk.Button(text='9', bd=2, font='黑体')
btn9.place( x=x0+x_width*2, y=y0, width=x_width, height=y_width)
# +
btn_add = self.tk.Button(text='+', bd=2, font='黑体')
btn_add.place( x=x0+x_width*3, y=y0, width=x_width, height=y_width)
# 4
btn4 = self.tk.Button(text='4', bd=2, font='黑体')
btn4.place( x=x0, y=y0+y_width, width=x_width, height=y_width)
# 5
btn5 = self.tk.Button(text='5', bd=2, font='黑体')
btn5.place( x=x0+x_width*1, y=y0+y_width, width=x_width, height=y_width)
# 6
btn6 = self.tk.Button(text='6', bd=2, font='黑体')
btn6.place( x=x0+x_width*2, y=y0+y_width, width=x_width, height=y_width)
# -
btn_subtract = self.tk.Button(text='-', bd=2, font='黑体')
btn_subtract.place( x=x0+x_width*3, y=y0+y_width, width=x_width, height=y_width)
# 1
btn1 = self.tk.Button(text='1', bd=2, font='黑体')
btn1.place( x=x0, y=y0+y_width*2, width=x_width, height=y_width)
# 2
btn2 = self.tk.Button(text='2', bd=2, font='黑体')
btn2.place( x=x0+x_width*1, y=y0+y_width*2, width=x_width, height=y_width)
# 3
btn3 = self.tk.Button(text='3', bd=2, font='黑体')
btn3.place( x=x0+x_width*2, y=y0+y_width*2, width=x_width, height=y_width)
# *
btn_mutiply = self.tk.Button(text='*', bd=2, font='黑体')
btn_mutiply.place( x=x0+x_width*3, y=y0+y_width*2, width=x_width, height=y_width)
# 0
btn0 = self.tk.Button(text='0', bd=2, font='黑体')
btn0.place( x=x0, y=y0+y_width*3, width=x_width*2, height=y_width)
# .
btn_point = self.tk.Button(text='.', bd=2, font='黑体')
btn_point.place( x=x0+x_width*2, y=y0+y_width*3, width=x_width, height=y_width)
# /
btn_divid = self.tk.Button(text='/', bd=2, font='黑体')
btn_divid.place( x=x0+x_width*3, y=y0+y_width*3, width=x_width, height=y_width)
# C后退
btn_back = self.tk.Button( text='C', bd=2, font='黑体')
btn_back.place( x=x0, y=y0+y_width*4, width=x_width*1, height=y_width)
# =
btn_equal = self.tk.Button( text='=', bd=2, font='黑体')
btn_equal.place( x=x0+x_width*1, y=y0+y_width*4, width=x_width*3, height=y_width)
# 绑定Button的点击事件
btn7.bind_class('Button', '<Button-1>', self.click_button)
def click_button(self, event):
# 输入的按键信息
input_event = event.widget['text']
# 输入的按键信息显示
self.label_show['text'] = self.label_show['text'] + input_event
# 进行普通计算
if event.widget['text'] == '=':
res_bit = 2 # 计算保留的位数
res = eval(self.label_show['text'][:-1]) # 计算点击 “=” 之前的计算表达式
# print(type(res))
self.label_show['text'] = str(round(float(res), res_bit ))
start = Calculator()
运行效果图4:
# ------------------------------------------------分割线-------------------------------------------------
'''
# ------------------------------------------------------------
# # 5、实现基本的计算器功能,补充错误检测
# # # 改动位置位置: def click_button(self, event):
# ------------------------------------------------------------
'''
class Calculator:
import tkinter as tk
import tkinter.messagebox as mbox
def __init__(self):
# 生成一个窗口对象
self.window = self.tk.Tk()
# 命名窗口对象的显示title
self.window.title('计算器')
# 设置窗口的大小 minsize最小 maxsize最大
self.window.minsize(240, 290)
self.window.maxsize(240, 290)
# 是否清空显示框判定参数
# ????
# 设置菜单
self.set_menu()
# 设置显示框
self.label_show = self.tk.Label(text='', anchor='se', font=('黑体', 30), fg='black')
self.label_show.place(x=0, y=0, width=240,height=80)
# 设置按钮组件
self.set_buttons()
# 将窗口放入主消息队列
self.window.mainloop()
def set_menu(self):
'''
设置菜单
:return: None
'''
# 创建总菜单
menubar = self.tk.Menu(self.window)
# 创建一个下拉菜单,并且加入文件菜单
filemenu = self.tk.Menu(menubar, tearoff=False)
# 创建菜单中的选项
filemenu.add_command(label='退出计算器', command=self.window.quit )
# print author的函数
def show_author():
self.mbox.showinfo(title='作者信息',message='作者:JY小脚丫\n联系邮箱:******print@foxmail.com')
filemenu.add_command(label='作者信息', command=show_author)
# 将文件菜单作为下拉菜单添加到总菜单中,并且将命名为操作
menubar.add_cascade(label='查看', menu=filemenu)
# 显示总菜单
self.window.config(menu=menubar)
def set_buttons(self):
# 基础坐标, x0, y0,开始坐标; x_width, y_width 间隔
x0, y0, x_width, y_width,height = 0, 90, 60, 40, 40
# 7
btn7 = self.tk.Button(text='7', bd=2, font='黑体')
btn7.place( x=x0, y=y0, width=x_width, height=y_width)
# 8
btn8 = self.tk.Button(text='8', bd=2, font='黑体')
btn8.place( x=x0+x_width*1, y=y0, width=x_width, height=y_width)
# 9
btn9 = self.tk.Button(text='9', bd=2, font='黑体')
btn9.place( x=x0+x_width*2, y=y0, width=x_width, height=y_width)
# +
btn_add = self.tk.Button(text='+', bd=2, font='黑体')
btn_add.place( x=x0+x_width*3, y=y0, width=x_width, height=y_width)
# 4
btn4 = self.tk.Button(text='4', bd=2, font='黑体')
btn4.place( x=x0, y=y0+y_width, width=x_width, height=y_width)
# 5
btn5 = self.tk.Button(text='5', bd=2, font='黑体')
btn5.place( x=x0+x_width*1, y=y0+y_width, width=x_width, height=y_width)
# 6
btn6 = self.tk.Button(text='6', bd=2, font='黑体')
btn6.place( x=x0+x_width*2, y=y0+y_width, width=x_width, height=y_width)
# -
btn_subtract = self.tk.Button(text='-', bd=2, font='黑体')
btn_subtract.place( x=x0+x_width*3, y=y0+y_width, width=x_width, height=y_width)
# 1
btn1 = self.tk.Button(text='1', bd=2, font='黑体')
btn1.place( x=x0, y=y0+y_width*2, width=x_width, height=y_width)
# 2
btn2 = self.tk.Button(text='2', bd=2, font='黑体')
btn2.place( x=x0+x_width*1, y=y0+y_width*2, width=x_width, height=y_width)
# 3
btn3 = self.tk.Button(text='3', bd=2, font='黑体')
btn3.place( x=x0+x_width*2, y=y0+y_width*2, width=x_width, height=y_width)
# *
btn_mutiply = self.tk.Button(text='*', bd=2, font='黑体')
btn_mutiply.place( x=x0+x_width*3, y=y0+y_width*2, width=x_width, height=y_width)
# 0
btn0 = self.tk.Button(text='0', bd=2, font='黑体')
btn0.place( x=x0, y=y0+y_width*3, width=x_width*2, height=y_width)
# .
btn_point = self.tk.Button(text='.', bd=2, font='黑体')
btn_point.place( x=x0+x_width*2, y=y0+y_width*3, width=x_width, height=y_width)
# /
btn_divid = self.tk.Button(text='/', bd=2, font='黑体')
btn_divid.place( x=x0+x_width*3, y=y0+y_width*3, width=x_width, height=y_width)
# C后退
btn_back = self.tk.Button( text='C', bd=2, font='黑体')
btn_back.place( x=x0, y=y0+y_width*4, width=x_width*1, height=y_width)
# =
btn_equal = self.tk.Button( text='=', bd=2, font='黑体')
btn_equal.place( x=x0+x_width*1, y=y0+y_width*4, width=x_width*3, height=y_width)
# 绑定Button的点击事件
btn7.bind_class('Button', '<Button-1>', self.click_button)
def click_button(self, event):
# 输入的按键信息
input_event = event.widget['text']
# 输入的按键信息显示
self.label_show['text'] = self.label_show['text'] + input_event
# 进行普通计算
if event.widget['text'] == '=':
try:
res_bit = 2 # 计算保留的位数
res = eval(self.label_show['text'][:-1]) # 计算点击 “=” 之前的计算表达式
# print(type(res))
self.label_show['text'] = str(round(float(res), res_bit))
except ZeroDivisionError:
# 除法时,除数不能为0
self.mbox.showerror(title='错误', message='除法计算时!除数不能为0!')
except:
self.mbox.showerror(title='未知名错误', message='算式错误,请检查!')
start = Calculator()
运行效果图5:
# ------------------------------------------------分割线-------------------------------------------------
'''
# ------------------------------------------------------------
# # 6、实现基本的计算器功能,补充错误检测,补充‘C’后退输入功能
# # # 改动位置位置: def click_button(self, event):
# ------------------------------------------------------------
'''
class Calculator:
import tkinter as tk
import tkinter.messagebox as mbox
def __init__(self):
# 生成一个窗口对象
self.window = self.tk.Tk()
# 命名窗口对象的显示title
self.window.title('计算器')
# 设置窗口的大小 minsize最小 maxsize最大
self.window.minsize(240, 290)
self.window.maxsize(240, 290)
# 是否清空显示框判定参数
# ????
# 设置菜单
self.set_menu()
# 设置显示框
self.label_show = self.tk.Label(text='', anchor='se', font=('黑体', 30), fg='black')
self.label_show.place(x=0, y=0, width=240,height=80)
# 设置按钮组件
self.set_buttons()
# 将窗口放入主消息队列
self.window.mainloop()
def set_menu(self):
'''
设置菜单
:return: None
'''
# 创建总菜单
menubar = self.tk.Menu(self.window)
# 创建一个下拉菜单,并且加入文件菜单
filemenu = self.tk.Menu(menubar, tearoff=False)
# 创建菜单中的选项
filemenu.add_command(label='退出计算器', command=self.window.quit )
# print author的函数
def show_author():
self.mbox.showinfo(title='作者信息',message='作者:JY小脚丫\n联系邮箱:******print@foxmail.com')
filemenu.add_command(label='作者信息', command=show_author)
# 将文件菜单作为下拉菜单添加到总菜单中,并且将命名为操作
menubar.add_cascade(label='查看', menu=filemenu)
# 显示总菜单
self.window.config(menu=menubar)
def set_buttons(self):
# 基础坐标, x0, y0,开始坐标; x_width, y_width 间隔
x0, y0, x_width, y_width,height = 0, 90, 60, 40, 40
# 7
btn7 = self.tk.Button(text='7', bd=2, font='黑体')
btn7.place( x=x0, y=y0, width=x_width, height=y_width)
# 8
btn8 = self.tk.Button(text='8', bd=2, font='黑体')
btn8.place( x=x0+x_width*1, y=y0, width=x_width, height=y_width)
# 9
btn9 = self.tk.Button(text='9', bd=2, font='黑体')
btn9.place( x=x0+x_width*2, y=y0, width=x_width, height=y_width)
# +
btn_add = self.tk.Button(text='+', bd=2, font='黑体')
btn_add.place( x=x0+x_width*3, y=y0, width=x_width, height=y_width)
# 4
btn4 = self.tk.Button(text='4', bd=2, font='黑体')
btn4.place( x=x0, y=y0+y_width, width=x_width, height=y_width)
# 5
btn5 = self.tk.Button(text='5', bd=2, font='黑体')
btn5.place( x=x0+x_width*1, y=y0+y_width, width=x_width, height=y_width)
# 6
btn6 = self.tk.Button(text='6', bd=2, font='黑体')
btn6.place( x=x0+x_width*2, y=y0+y_width, width=x_width, height=y_width)
# -
btn_subtract = self.tk.Button(text='-', bd=2, font='黑体')
btn_subtract.place( x=x0+x_width*3, y=y0+y_width, width=x_width, height=y_width)
# 1
btn1 = self.tk.Button(text='1', bd=2, font='黑体')
btn1.place( x=x0, y=y0+y_width*2, width=x_width, height=y_width)
# 2
btn2 = self.tk.Button(text='2', bd=2, font='黑体')
btn2.place( x=x0+x_width*1, y=y0+y_width*2, width=x_width, height=y_width)
# 3
btn3 = self.tk.Button(text='3', bd=2, font='黑体')
btn3.place( x=x0+x_width*2, y=y0+y_width*2, width=x_width, height=y_width)
# *
btn_mutiply = self.tk.Button(text='*', bd=2, font='黑体')
btn_mutiply.place( x=x0+x_width*3, y=y0+y_width*2, width=x_width, height=y_width)
# 0
btn0 = self.tk.Button(text='0', bd=2, font='黑体')
btn0.place( x=x0, y=y0+y_width*3, width=x_width*2, height=y_width)
# .
btn_point = self.tk.Button(text='.', bd=2, font='黑体')
btn_point.place( x=x0+x_width*2, y=y0+y_width*3, width=x_width, height=y_width)
# /
btn_divid = self.tk.Button(text='/', bd=2, font='黑体')
btn_divid.place( x=x0+x_width*3, y=y0+y_width*3, width=x_width, height=y_width)
# C后退
btn_back = self.tk.Button( text='C', bd=2, font='黑体')
btn_back.place( x=x0, y=y0+y_width*4, width=x_width*1, height=y_width)
# =
btn_equal = self.tk.Button( text='=', bd=2, font='黑体')
btn_equal.place( x=x0+x_width*1, y=y0+y_width*4, width=x_width*3, height=y_width)
# 绑定Button的点击事件
btn7.bind_class('Button', '<Button-1>', self.click_button)
def click_button(self, event):
# 输入的按键信息
input_event = event.widget['text']
# 输入的按键信息显示
self.label_show['text'] = self.label_show['text'] + input_event
# 进行普通计算
if event.widget['text'] == '=':
try:
res_bit = 2 # 计算保留的位数
res = eval(self.label_show['text'][:-1]) # 计算点击 “=” 之前的计算表达式
# print(type(res))
self.label_show['text'] = str(round(float(res), res_bit))
except ZeroDivisionError:
# 除法时,除数不能为0
self.mbox.showerror(title='错误', message='除法计算时!除数不能为0!')
except:
self.mbox.showerror(title='未知名错误', message='算式错误,请检查!')
elif event.widget['text'] == 'C':
back_res = self.label_show['text'][:-2] # 点击的‘C’也计算在内,因此是倒数2位去掉
self.label_show['text'] = back_res
start = Calculator()
运行效果图6:
# ------------------------------------------------分割线-------------------------------------------------
'''
# ------------------------------------------------------------
# # 7、(完成全部设计)实现基本的计算器功能,补充错误检测, 修改‘C’为清楚功能, '<=='为后退功能
# # # 改动位置位置: def click_button(self, event):
# ------------------------------------------------------------
'''
class Calculator:
import tkinter as tk
import tkinter.messagebox as mbox
def __init__(self):
# 生成一个窗口对象
self.window = self.tk.Tk()
# 命名窗口对象的显示title
self.window.title('计算器')
# 设置窗口的大小 minsize最小 maxsize最大
self.window.minsize(240, 290)
self.window.maxsize(240, 290)
# 是否清空显示框判定参数
# ????
# 设置菜单
self.set_menu()
# 设置显示框
self.label_show = self.tk.Label(text='', anchor='se', font=('黑体', 30), fg='black')
self.label_show.place(x=0, y=0, width=240,height=80)
# 设置按钮组件
self.set_buttons()
# 将窗口放入主消息队列
self.window.mainloop()
def set_menu(self):
'''
设置菜单
:return: None
'''
# 创建总菜单
menubar = self.tk.Menu(self.window)
# 创建一个下拉菜单,并且加入文件菜单
filemenu = self.tk.Menu(menubar, tearoff=False)
# 创建菜单中的选项
filemenu.add_command(label='退出计算器', command=self.window.quit )
# print author的函数
def show_author():
self.mbox.showinfo(title='作者信息',message='作者:JY小脚丫\n联系邮箱:******print@foxmail.com')
filemenu.add_command(label='作者信息', command=show_author)
# 将文件菜单作为下拉菜单添加到总菜单中,并且将命名为操作
menubar.add_cascade(label='查看', menu=filemenu)
# 显示总菜单
self.window.config(menu=menubar)
def set_buttons(self):
# 基础坐标, x0, y0,开始坐标; x_width, y_width 间隔
x0, y0, x_width, y_width,height = 0, 90, 60, 40, 40
# 7
btn7 = self.tk.Button(text='7', bd=2, font='黑体')
btn7.place( x=x0, y=y0, width=x_width, height=y_width)
# 8
btn8 = self.tk.Button(text='8', bd=2, font='黑体')
btn8.place( x=x0+x_width*1, y=y0, width=x_width, height=y_width)
# 9
btn9 = self.tk.Button(text='9', bd=2, font='黑体')
btn9.place( x=x0+x_width*2, y=y0, width=x_width, height=y_width)
# +
btn_add = self.tk.Button(text='+', bd=2, font='黑体')
btn_add.place( x=x0+x_width*3, y=y0, width=x_width, height=y_width)
# 4
btn4 = self.tk.Button(text='4', bd=2, font='黑体')
btn4.place( x=x0, y=y0+y_width, width=x_width, height=y_width)
# 5
btn5 = self.tk.Button(text='5', bd=2, font='黑体')
btn5.place( x=x0+x_width*1, y=y0+y_width, width=x_width, height=y_width)
# 6
btn6 = self.tk.Button(text='6', bd=2, font='黑体')
btn6.place( x=x0+x_width*2, y=y0+y_width, width=x_width, height=y_width)
# -
btn_subtract = self.tk.Button(text='-', bd=2, font='黑体')
btn_subtract.place( x=x0+x_width*3, y=y0+y_width, width=x_width, height=y_width)
# 1
btn1 = self.tk.Button(text='1', bd=2, font='黑体')
btn1.place( x=x0, y=y0+y_width*2, width=x_width, height=y_width)
# 2
btn2 = self.tk.Button(text='2', bd=2, font='黑体')
btn2.place( x=x0+x_width*1, y=y0+y_width*2, width=x_width, height=y_width)
# 3
btn3 = self.tk.Button(text='3', bd=2, font='黑体')
btn3.place( x=x0+x_width*2, y=y0+y_width*2, width=x_width, height=y_width)
# *
btn_mutiply = self.tk.Button(text='*', bd=2, font='黑体')
btn_mutiply.place( x=x0+x_width*3, y=y0+y_width*2, width=x_width, height=y_width)
# 0
btn0 = self.tk.Button(text='0', bd=2, font='黑体')
btn0.place( x=x0, y=y0+y_width*3, width=x_width*2, height=y_width)
# .
btn_point = self.tk.Button(text='.', bd=2, font='黑体')
btn_point.place( x=x0+x_width*2, y=y0+y_width*3, width=x_width, height=y_width)
# /
btn_divid = self.tk.Button(text='/', bd=2, font='黑体')
btn_divid.place( x=x0+x_width*3, y=y0+y_width*3, width=x_width, height=y_width)
# C后退
btn_clear = self.tk.Button( text='C', bd=2, font='黑体')
btn_clear.place( x=x0, y=y0+y_width*4, width=x_width, height=y_width)
# C后退
btn_back = self.tk.Button( text='<==', bd=2, font='黑体')
btn_back.place( x=x0+x_width*1, y=y0+y_width*4, width=x_width, height=y_width)
# =
btn_equal = self.tk.Button( text='=', bd=2, font='黑体')
btn_equal.place( x=x0+x_width*2, y=y0+y_width*4, width=x_width*2, height=y_width)
# 绑定Button的点击事件
btn7.bind_class('Button', '<Button-1>', self.click_button)
def click_button(self, event):
# 获取点击的按钮信息信息
input_event = event.widget['text']
# 输入的按钮信息显示
self.label_show['text'] = self.label_show['text'] + input_event
# 异常捕获
try:
# 计算符号
cal_symbol = ['+', '-', '*', '/']
# 判定运算符号重复的时候,使用最后输入的符号
if self.label_show['text'][-1] in cal_symbol and self.label_show['text'][-2] in cal_symbol:
# 取重复符号前面的内容
header = self.label_show['text'][:-2]
footer = self.label_show['text'][-1]
self.label_show['text'] = header + footer
except:
pass
# 进行普通计算
if event.widget['text'] == '=':
# 异常捕获
try:
res_bit = 2 # 计算保留的位数
res = eval(self.label_show['text'][:-1]) # 计算点击 “=” 之前的计算表达式
# print(type(res))
self.label_show['text'] = str(round(float(res), res_bit))
except ZeroDivisionError:
# 除法时,除数不能为0
self.mbox.showerror(title='错误', message='除法计算时!除数不能为0!')
except:
self.mbox.showerror(title='未知名错误', message='算式错误,请检查!')
elif event.widget['text'] == '<==':
# 点击的‘<==’也计算在内,它占3个字符,因此是倒数4位去掉
back_res = self.label_show['text'][:-4]
self.label_show['text'] = back_res
elif event.widget['text'] == 'C':
# 点击的‘C’也计算在内,因此是倒数2位去掉
self.label_show['text'] = ''
start = Calculator()
运行效果图7(最终的运行效果):
转载请注明出处。https://www.cnblogs.com/jyfootprint/p/9457346.html