欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python制作简易计算器

程序员文章站 2022-04-02 11:22:02
...

运行预览图:

Python制作简易计算器

from tkinter import *
root = Tk()
root.title("简易计算器")
root.geometry("370x480")    #窗体大小

result = StringVar()
result.set('')
# lab.pack()

# 标记,记录是否已经进行运算
flag = 0;

# 获得按下的字符
def getnum(num):
    global flag
    temp=result.get()
    result.set(temp)
    # 已经进行过运算,清空result这个Label标签
    if flag != 0:
        temp = ' '
        flag = 0
        result.set(temp)
    temp = temp + num
    result.set(temp)

# 删掉最后一个字符
def delete():
    temp = result.get()
    result.set(temp[:-1])

# 重置
def reset():
    result.set(' ')

# 计算
def run():
    temp = result.get()
    result.set(' ')
    temp = temp.replace('x', '*')
    temp = temp.replace('÷', '/')

    answer =eval(temp)
    result.set(str(answer))
    # 记录已经进行了运算
    global flag
    flag = 1

lab = Label(root,text="",bg="yellow",width=18,height=2,font=('宋体','30'),textvariable=result, anchor='se')
lab.grid(row=0,column=0,columnspan=4)

# 计算器按钮
# 第一行
btn_delete = Button(root,text="←",width=4,height=1,font=('宋体','25'),command=delete)
btn_left = Button(root,text="(",width=4,height=1,font=('宋体','25'),command=lambda: getnum('('))
btn_right = Button(root,text=")",width=4,height=1,font=('宋体','25'),command=lambda: getnum(')'))
btnm = Button(root,text="x",width=4,height=1,font=('宋体','25'),command=lambda: getnum('x'))
btn_delete.grid(row=1,column=0,padx=5,pady=10)
btn_left.grid(row=1,column=1,padx=5,pady=10)
btn_right.grid(row=1,column=2,padx=5,pady=10)
btnm.grid(row=1,column=3,padx=5,pady=10)

#第二行
btn7 = Button(root,text="7",width=4,height=1,font=('宋体','25'),command=lambda: getnum('7'))
btn8 = Button(root,text="8",width=4,height=1,font=('宋体','25'),command=lambda: getnum('8'))
btn9 = Button(root,text="9",width=4,height=1,font=('宋体','25'),command=lambda: getnum('9'))
btnd = Button(root,text="÷",width=4,height=1,font=('宋体','25'),command=lambda: getnum('÷'))
btn7.grid(row=2,column=0,padx=5,pady=10)
btn8.grid(row=2,column=1,padx=5,pady=10)
btn9.grid(row=2,column=2,padx=5,pady=10)
btnd.grid(row=2,column=3,padx=5,pady=10)

# 第三行
btn4 = Button(root,text="4",width=4,height=1,font=('宋体','25'),command=lambda: getnum('4'))
btn5 = Button(root,text="5",width=4,height=1,font=('宋体','25'),command=lambda: getnum('5'))
btn6 = Button(root,text="6",width=4,height=1,font=('宋体','25'),command=lambda: getnum('6'))
btns = Button(root,text="-",width=4,height=1,font=('宋体','25'),command=lambda: getnum('-'))
btn4.grid(row=3,column=0,padx=5,pady=10)
btn5.grid(row=3,column=1,padx=5,pady=10)
btn6.grid(row=3,column=2,padx=5,pady=10)
btns.grid(row=3,column=3,padx=5,pady=10)

# 第四行
btn1 = Button(root,text="1",width=4,height=1,font=('宋体','25'),command=lambda: getnum('1'))
btn2 = Button(root,text="2",width=4,height=1,font=('宋体','25'),command=lambda: getnum('2'))
btn3 = Button(root,text="3",width=4,height=1,font=('宋体','25'),command=lambda: getnum('3'))
btna = Button(root,text="+",width=4,height=1,font=('宋体','25'),command=lambda: getnum('+'))
btn1.grid(row=4,column=0,padx=5,pady=10)
btn2.grid(row=4,column=1,padx=5,pady=10)
btn3.grid(row=4,column=2,padx=5,pady=10)
btna.grid(row=4,column=3,padx=5,pady=10)

# 第五行
btn0 = Button(root,text="0",width=4,height=1,font=('宋体','25'),command=lambda: getnum('0'))
btn_dot = Button(root,text=".",width=4,height=1,font=('宋体','25'),command=lambda: getnum('.'))
btn_equal = Button(root,text="=",width=4,height=1,font=('宋体','25'),command=run)
btnz = Button(root,text="C",width=4,height=1,font=('宋体','25'),command=reset)
btnz.grid(row=5,column=0,padx=5,pady=10)
btn0.grid(row=5,column=1,padx=5,pady=10)
btn_dot.grid(row=5,column=2,padx=5,pady=10)
btn_equal.grid(row=5,column=3,padx=5,pady=10)

root.mainloop()     #让窗体不断循环,以达到显示效果,否者会瞬间消失