做一个计算机

计算机是一种能够接收、处理和输出信息的智能电子设备。它被广泛应用于各行各业,包括科学、教育、工程、医疗、商业等领域。它的核心部分是中央处理器(CPU),它能够执行各种指令并进行算术和逻辑操作。除了CPU之外,计算机还包括内存、硬盘、输入输出设备、显示器等部件。计算机通过运行各种软件程序来实现各种功能,这些软件程序可以是操作系统、应用程序、游戏等。计算机的发展历程非常漫长,从最初的机械计算器到现在的高性能电子计算机,计算机已经成为了人类文明的一个重要组成部分。

当然啦,计算机不会计算怎么行呢,今天,咱们就来做一个计算机吧!!

源码:

import tkinter
window = tkinter.Tk()
window.title("计算器")
expstr = ""
history_label_obj_list = []
def onClick(key):
    global  expstr  
    if key == "=":
        jieguo = round(eval(expstr), 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")

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)


ac = tkinter.Button(frame_left, text="AC", width=6, height=2, command=lambda: onClick("AC"))
ac.grid(row=1,column=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()

不喜勿喷

下期见!!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值