在学习Python语言的过程中,大家都希望有一些精美的例子进行尝试和练习。
本文,提供了完整的计算器的Python源代码,希望对大家有所帮助,以下是完整源代码,供大家使用!
# -*-coding:utf_8-*-
import math
from tkinter import *
# 主窗口句柄
root = Tk()
# 全局变量
CurrentShow =StringVar(value='0');
MAXSHOWLEN = 20;
IS_CALC = True;
STORAGE = []
def press_number(number):
"""
按下数字按键
:param number: 具体的数字
"""
global IS_CALC
# 如果是计算,显示值设为0
if IS_CALC:
CurrentShow.set('0')
IS_CALC = False
# 如果内容为0,那么直接设置为该数字
if CurrentShow.get() == '0':
CurrentShow.set(number)
# 否则如何在规定范围内,将数字进行拼接
else:
if len(CurrentShow.get()) < MAXSHOWLEN:
CurrentShow.set(CurrentShow.get() + number)
def press_dp():
"""
是否输入小数点
:return: 无
"""
global IS_CALC
# 如果是计算,显示值设为0
if IS_CALC:
CurrentShow.set('0')
IS_CALC = False
# 如果输入小数点,且是第一个小数点进行显示
if len(CurrentShow.get().split('.')) == 1:
if len(CurrentShow.get()) < MAXSHOWLEN:
CurrentShow.set(CurrentShow.get() + '.')
def clear_all():
"""
清空所有
:return: 无
"""
global STORAGE
global IS_CALC
STORAGE.clear()
IS_CALC = False
CurrentShow.set('0')
def clear_current():
"""
清除当前显示框内所有数字
:return: 无
"""
CurrentShow.set('0')
def del_one():
"""
删除单个数字
:return: 无
"""
global IS_CALC
# 如果是计算,显示值设为0
if IS_CALC:
CurrentShow.set('0')
IS_CALC = False
# 如果有值
if CurrentShow.get() != '0':
# 如果长度大于1,删除掉最后一个字符
if len(CurrentShow.get()) > 1:
CurrentShow.set(CurrentShow.get()[:-1])
else:
# 如果长度小于等于1,直接设置为0
CurrentShow.set('0')
def modify_result(result):
"""
修正结果
:param result: 待处理的结果
:return: 修正后的结果
"""
result = str(result)
# 超出了固定的长度
if len(result) > MAXSHOWLEN:
if len(result.split('.')[0]) > MAXSHOWLEN:
result = '超出范围了'
else:
# 直接舍去不考虑四舍五入问题
result = result[:MAXSHOWLEN]
return result
def press_operator(operator):
"""
处理运算符情况
:param operator: 点击的运算符
:return: 无
"""
global STORAGE
global IS_CALC
# 处理正负号
if operator == '+/-':
if CurrentShow.get().startswith('-'):
CurrentShow.set(CurrentShow.get()[1:])
else:
CurrentShow.set('-' + CurrentShow.get())
# 处理倒数
elif operator == '1/x':
try:
result = 1 / float(CurrentShow.get())
except:
result = '无效操作'
result = modify_result(result)
CurrentShow.set(result)
IS_CALC = True
# 处理开方
elif operator == 'sqrt':
try:
result = math.sqrt(float(CurrentShow.get()))
except:
result = '无效操作'
result = modify_result(result)
CurrentShow.set(result)
IS_CALC = True
# 清除存储
elif operator == 'MC':
STORAGE.clear()
# 记录
elif operator == 'MR':
if IS_CALC:
CurrentShow.set('0')
STORAGE.append(CurrentShow.get())
expression = ''.join(STORAGE)
try:
result = eval(expression)
except:
result = '无效操作'
result = modify_result(result)
CurrentShow.set(result)
IS_CALC = True
# 清除存储再记录
elif operator == 'MS':
STORAGE.clear()
STORAGE.append(CurrentShow.get())
# 追加存储
elif operator == 'M+':
STORAGE.append(CurrentShow.get())
# 追加存储
elif operator == 'M-':
if CurrentShow.get().startswith('-'):
STORAGE.append(CurrentShow.get())
else:
STORAGE.append('-' + CurrentShow.get())
# 处理加减乘除
elif operator in ['+', '-', '*', '/', '%']:
STORAGE.append(CurrentShow.get())
STORAGE.append(operator)
IS_CALC = True
# 处理计算
elif operator == '=':
if IS_CALC:
CurrentShow.set('0')
STORAGE.append(CurrentShow.get())
expression = ''.join(STORAGE)
try:
result = eval(expression)
# 除以0的情况
except:
result = '无效操作'
result = modify_result(result)
CurrentShow.set(result)
STORAGE.clear()
IS_CALC = True
def calc():
"""
计算器初始化
:return:
"""
root.minsize(320, 380)
root.title('毛毛计算器')
# 布局
# --文本框
label = Label(root, textvariable=CurrentShow, bg='#7A7A7A', anchor='e', bd=5, fg='white', font=('楷体', 20))
label.place(x=20, y=20, width=280, height=50)
# --第一行
# ----Memory clear
button1_1 = Button(text='MC', bg='#f0f0f0', bd=2, font=('楷体', 13), command=lambda: press_operator('MC'))
button1_1.place(x=20, y=80, width=50, height=35)
# ----Memory read
button1_2 = Button(text='MR', bg='#f0f0f0', bd=2, font=('楷体', 13), command=lambda: press_operator('MR'))
button1_2.place(x=77.5, y=80, width=50, height=35)
# ----Memory save
button1_3 = Button(text='MS', bg='#f0f0f0', bd=2, font=('楷体', 13), command=lambda: press_operator('MS'))
button1_3.place(x=135, y=80, width=50, height=35)
# ----Memory +
button1_4 = Button(text='M+', bg='#f0f0f0', bd=2, font=('楷体', 13), command=lambda: press_operator('M+'))
button1_4.place(x=192.5, y=80, width=50, height=35)
# ----Memory -
button1_5 = Button(text='M-', bg='#f0f0f0', bd=2, font=('楷体', 13), command=lambda: press_operator('M-'))
button1_5.place(x=250, y=80, width=50, height=35)
# --第二行
# ----删除单个数字
button2_1 = Button(text='DEL', bg='#f0f0f0', bd=2, font=('楷体', 13), command=lambda: del_one())
button2_1.place(x=20, y=125, width=50, height=35)
# ----清除当前显示框内所有数字
button2_2 = Button(text='CE', bg='#f0f0f0', bd=2, font=('楷体', 13), command=lambda: clear_current())
button2_2.place(x=77.5, y=125, width=50, height=35)
# ----清零(相当于重启)
button2_3 = Button(text='C', bg='#f0f0f0', bd=2, font=('楷体', 13), command=lambda: clear_all())
button2_3.place(x=135, y=125, width=50, height=35)
# ----取反
button2_4 = Button(text='+/-', bg='#f0f0f0', bd=2, font=('楷体', 13), command=lambda: press_operator('+/-'))
button2_4.place(x=192.5, y=125, width=50, height=35)
# ----开根号
button2_5 = Button(text='√ ̄', bg='#f0f0f0', bd=2, font=('楷体', 13), command=lambda: press_operator('sqrt'))
button2_5.place(x=250, y=125, width=50, height=35)
# --第三行
# ----7
button3_1 = Button(text='7', bg='#EE7600', bd=2, font=('楷体', 15), command=lambda: press_number('7'))
button3_1.place(x=20, y=170, width=50, height=35)
# ----8
button3_2 = Button(text='8', bg='#EE7600', bd=2, font=('楷体', 15), command=lambda: press_number('8'))
button3_2.place(x=77.5, y=170, width=50, height=35)
# ----9
button3_3 = Button(text='9', bg='#EE7600', bd=2, font=('楷体', 15), command=lambda: press_number('9'))
button3_3.place(x=135, y=170, width=50, height=35)
# ----除
button3_4 = Button(text='/', bg='#B4EEB4', bd=2, font=('楷体', 15), command=lambda: press_operator('/'))
button3_4.place(x=192.5, y=170, width=50, height=35)
# ----取余
button3_5 = Button(text='%', bg='#B4EEB4', bd=2, font=('楷体', 15), command=lambda: press_operator('%'))
button3_5.place(x=250, y=170, width=50, height=35)
# --第四行
# ----4
button4_1 = Button(text='4', bg='#EE7600', bd=2, font=('楷体', 15), command=lambda: press_number('4'))
button4_1.place(x=20, y=215, width=50, height=35)
# ----5
button4_2 = Button(text='5', bg='#EE7600', bd=2, font=('楷体', 15), command=lambda: press_number('5'))
button4_2.place(x=77.5, y=215, width=50, height=35)
# ----6
button4_3 = Button(text='6', bg='#EE7600', bd=2, font=('楷体', 15), command=lambda: press_number('6'))
button4_3.place(x=135, y=215, width=50, height=35)
# ----乘
button4_4 = Button(text='*', bg='#B4EEB4', bd=2, font=('楷体', 15), command=lambda: press_operator('*'))
button4_4.place(x=192.5, y=215, width=50, height=35)
# ----取导数
button4_5 = Button(text='1/x', bg='#B4EEB4', bd=2, font=('楷体', 15), command=lambda: press_operator('1/x'))
button4_5.place(x=250, y=215, width=50, height=35)
# --第五行
# ----3
button5_1 = Button(text='3', bg='#EE7600', bd=2, font=('楷体', 15), command=lambda: press_number('3'))
button5_1.place(x=20, y=260, width=50, height=35)
# ----2
button5_2 = Button(text='2', bg='#EE7600', bd=2, font=('楷体', 15), command=lambda: press_number('2'))
button5_2.place(x=77.5, y=260, width=50, height=35)
# ----1
button5_3 = Button(text='1', bg='#EE7600', bd=2, font=('楷体', 15), command=lambda: press_number('1'))
button5_3.place(x=135, y=260, width=50, height=35)
# ----减
button5_4 = Button(text='-', bg='#B4EEB4', bd=2, font=('楷体', 15), command=lambda: press_operator('-'))
button5_4.place(x=192.5, y=260, width=50, height=35)
# ----等于
button5_5 = Button(text='=', bg='red', bd=2, font=('楷体', 20), command=lambda: press_operator('='))
button5_5.place(x=250, y=260, width=50, height=80)
# --第六行
# ----0
button6_1 = Button(text='0', bg='#EE7600', bd=2, font=('楷体', 15), command=lambda: press_number('0'))
button6_1.place(x=20, y=305, width=107.5, height=35)
# ----小数点
button6_2 = Button(text='.', bg='#EE7600', bd=2, font=('楷体', 15), command=lambda: press_dp())
button6_2.place(x=135, y=305, width=50, height=35)
# ----加
button6_3 = Button(text='+', bg='#B4EEB4', bd=2, font=('楷体', 15), command=lambda: press_operator('+'))
button6_3.place(x=192.5, y=305, width=50, height=35)
root.mainloop()
# 按间距中的绿色按钮以运行脚本。
if __name__ == '__main__':
calc()
运行后的效果图如下: