手把手教你用python制作简易计算器,能够记录你使用的情况

看前点个赞、加个关注是对我最大的鼓励
在这里插入图片描述

话不多说,首先先看效果图,它能够记录你在使用过程中的历史,方便你查看是否有错:
在这里插入图片描述

接下来就仔细分析一下是如何制作的:

第一步:导入资源库

在过程中使用到了tkinter这个资源库,win+R打开CMD输入:pip install python-tk

当然,没有安装Tkinter可以点击如下链接下载安装包:
http://wiki.python.org/moin/TkInter

python的引入方式为:import tkinter

第二步:创建窗口

定义窗口并设置窗口标题:

# 创建窗口
window = tkinter.Tk()
window.title("计算器")

第三步:变量初始化

设置算式记录变量以及运算历史变量的值:

# 记录算式
expstr = ""

# 记录运算历史
history_label_obj_list = []

第四步:定义按钮点击事件函数

计算器有很多按钮,当我们点击相应的按钮时会触发相应的事件,这时我们就需要定义一个按钮点击事件的函数。

# 按钮点击事件
def onClick(key):
    
    global  expstr  # 定义全局变量

    if key == "=":
        jieguo = round(eval(expstr), 2)# 结果保留2位小数
        result["text"] = jieguo

        frame_right.pack()
        
        # 将算式记录显示出来
        t = tkinter.Label(frame_inner, text=expstr + "=" + str(jieguo),
                            background="seashell")
        t.pack()

        history_label_obj_list.append(t)# 容器存储算式记录
    elif key == "AC":
        result["text"] = ""
        expstr = ""
    else:
        expstr = expstr + str(key)
        result["text"] = expstr

第五步:定义清空运算历史函数

该计算器包含有记录运算历史的功能,当我们点击清空的时候,会调用我们定义的清空运算历史的函数。

# 清空运算历史
def clean_history():

    for x in history_label_obj_list:
        print(x)
        x.destroy()

第六步:设置数字0-9按钮

将各个按钮设置长为6,宽为2,分别在frame_left。

数字7-9分布在第二行的第0-2列:

num7 = tkinter.Button(frame_left, text="7", width=6, height=2, command=lambda: onClick(7))
num7.grid(row=2,column=0)

num8 = tkinter.Button(frame_left, text="8", width=6, height=2, command=lambda: onClick(8))
num8.grid(row=2,column=1)

num9 = tkinter.Button(frame_left, text="9", width=6, height=2, command=lambda: onClick(9))
num9.grid(row=2,column=2)

数字4-6分布在第三行的第0-2列:

num4 = tkinter.Button(frame_left, text="4", width=6, height=2, command=lambda: onClick(4))
num4.grid(row=3,column=0)

num5 = tkinter.Button(frame_left, text="5", width=6, height=2, command=lambda: onClick(5))
num5.grid(row=3,column=1)

num6 = tkinter.Button(frame_left, text="6", width=6, height=2, command=lambda: onClick(6))
num6.grid(row=3,column=2)

数字1-3分布在第四行的第0-2列:

num1 = tkinter.Button(frame_left, text="1", width=6, height=2, command=lambda: onClick(1))
num1.grid(row=4,column=0)

num2 = tkinter.Button(frame_left, text="2", width=6, height=2, command=lambda: onClick(2))
num2.grid(row=4,column=1)

num3 = tkinter.Button(frame_left, text="3", width=6, height=2, command=lambda: onClick(3))
num3.grid(row=4,column=2)

数字0分布在第五行的第0列,长设置为12,宽为2:

num0 = tkinter.Button(frame_left, text="0", width=12, height=2, command=lambda: onClick(0))
num0.grid(row=5,column=0,columnspan=2)

第七步:设置运算符号按钮

该计算器的运算符号包括+、-、*、/、%、.,同时还包括一个清空按钮AC

negative = tkinter.Button(frame_left, text="+/-", width=6, height=2, command=lambda: onClick("-"))
negative.grid(row=1,column=1)

percent = tkinter.Button(frame_left, text="%", width=6, height=2, command=lambda: onClick("/100"))
percent.grid(row=1,column=2)

division = tkinter.Button(frame_left, text="/", width=6, height=2, command=lambda: onClick("/"))
division.grid(row=1,column=3)

multi = tkinter.Button(frame_left, text="*", width=6, height=2, command=lambda: onClick("*"))
multi.grid(row=2,column=3)

sub = tkinter.Button(frame_left, text="-", width=6, height=2, command=lambda: onClick("-"))
sub.grid(row=3,column=3)

add = tkinter.Button(frame_left, text="+", width=6, height=2, command=lambda: onClick("+"))
add.grid(row=4,column=3)

point = tkinter.Button(frame_left, text=".", width=6, height=2, command=lambda: onClick("."))
point.grid(row=5,column=2)

equals = tkinter.Button(frame_left, text="=", width=6, height=2, command=lambda: onClick("="))
equals.grid(row=5,column=3)

第八步:运行结果

最初结果:
在这里插入图片描述
运算后结果:
在这里插入图片描述

第九步:完整代码

import tkinter

# 创建窗口
window = tkinter.Tk()
window.title("计算器")

# 记录算式
expstr = ""

# 记录运算历史
history_label_obj_list = []

# 按钮点击事件
def onClick(key):
    
    global  expstr  # 定义全局变量

    if key == "=":
        jieguo = round(eval(expstr), 2)# 结果保留2位小数
        result["text"] = jieguo

        frame_right.pack()
        
        # 将算式记录显示出来
        t = tkinter.Label(frame_inner, text=expstr + "=" + str(jieguo),
                            background="seashell")
        t.pack()

        history_label_obj_list.append(t)# 容器存储算式记录
    elif key == "AC":
        result["text"] = ""
        expstr = ""
    else:
        expstr = expstr + str(key)
        result["text"] = expstr

frame_grap = tkinter.Frame(window)
frame_grap.pack(fill="y", side="left")# 按y坐标填满放在左侧

frame_left = tkinter.Frame(window)

# 定义一个标签,设置相关参数,存放结果
result = tkinter.Label(frame_left, bg="seashell", text="0", height=2,font=("Arial", 34, "bold"))
result.grid(row=0, column=0, columnspan=4, sticky=tkinter.E)# 采用表格式布局管理器gid

# 设置“清空”按钮
ac = tkinter.Button(frame_left, text="AC", width=6, height=2, command=lambda: onClick("AC"))
ac.grid(row=1,column=0)# (第1行,第0列)

# 
negative = tkinter.Button(frame_left, text="+/-", width=6, height=2, command=lambda: onClick("-"))
negative.grid(row=1,column=1)

percent = tkinter.Button(frame_left, text="%", width=6, height=2, command=lambda: onClick("/100"))
percent.grid(row=1,column=2)

division = tkinter.Button(frame_left, text="/", width=6, height=2, command=lambda: onClick("/"))
division.grid(row=1,column=3)

num7 = tkinter.Button(frame_left, text="7", width=6, height=2, command=lambda: onClick(7))
num7.grid(row=2,column=0)

num8 = tkinter.Button(frame_left, text="8", width=6, height=2, command=lambda: onClick(8))
num8.grid(row=2,column=1)

num9 = tkinter.Button(frame_left, text="9", width=6, height=2, command=lambda: onClick(9))
num9.grid(row=2,column=2)

multi = tkinter.Button(frame_left, text="*", width=6, height=2, command=lambda: onClick("*"))
multi.grid(row=2,column=3)

num4 = tkinter.Button(frame_left, text="4", width=6, height=2, command=lambda: onClick(4))
num4.grid(row=3,column=0)

num5 = tkinter.Button(frame_left, text="5", width=6, height=2, command=lambda: onClick(5))
num5.grid(row=3,column=1)

num6 = tkinter.Button(frame_left, text="6", width=6, height=2, command=lambda: onClick(6))
num6.grid(row=3,column=2)

sub = tkinter.Button(frame_left, text="-", width=6, height=2, command=lambda: onClick("-"))
sub.grid(row=3,column=3)

num1 = tkinter.Button(frame_left, text="1", width=6, height=2, command=lambda: onClick(1))
num1.grid(row=4,column=0)

num2 = tkinter.Button(frame_left, text="2", width=6, height=2, command=lambda: onClick(2))
num2.grid(row=4,column=1)

num3 = tkinter.Button(frame_left, text="3", width=6, height=2, command=lambda: onClick(3))
num3.grid(row=4,column=2)

add = tkinter.Button(frame_left, text="+", width=6, height=2, command=lambda: onClick("+"))
add.grid(row=4,column=3)

num0 = tkinter.Button(frame_left, text="0", width=12, height=2, command=lambda: onClick(0))
num0.grid(row=5,column=0,columnspan=2)

point = tkinter.Button(frame_left, text=".", width=6, height=2, command=lambda: onClick("."))
point.grid(row=5,column=2)

equals = tkinter.Button(frame_left, text="=", width=6, height=2, command=lambda: onClick("="))
equals.grid(row=5,column=3)

frame_left.pack(fill="y", side="left")

frame_right = tkinter.Frame(window, width=200)
tkinter.Label(frame_right, text="运算历史", font=("Arial", 14, "underline bold")).pack()

frame_inner = tkinter.Frame(frame_right)
frame_inner.pack(fill="x", side="top")


# 清空运算历史
def clean_history():

    for x in history_label_obj_list:
        print(x)
        x.destroy()
        

cls_button = tkinter.Button(frame_right, text="清空", command=lambda: clean_history())
cls_button.pack(fill="x", side="top")

window.mainloop()

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/ywsydwsbn/article/details/107153074