python GUI界面计算器实现

from tkinter import *

root=Tk()
root.title("计算器")

Label(root,text="=").grid(row=0,column=3)

v = StringVar()
v1 = StringVar()

e1=Entry(root,justify='center')
e1.grid(row=0,column=0)

e2=Entry(root,width=5,textvariable=v1,state="readonly",justify='center')
e2.grid(row=0,column=1)

e3=Entry(root,justify='center')
e3.grid(row=0,column=2)

e4=Entry(root,textvariable=v,state="readonly",justify='center')
e4.grid(row=0,column=4)

def add():
    v1.set(str('+'))
    global a
    a=1
def jian():
    v1.set(str('-'))
    global a
    a=2
def chen():
    v1.set(str('*'))
    global a
    a=3
def chu():
    v1.set(str('/'))
    global a
    a=4
def ji():
    if a==1:
        num=int(e1.get())+int(e3.get())
    if a==2:
        num=int(e1.get())-int(e3.get())
    if a==3:
        num=int(e1.get())*int(e3.get())
    if a==4:
        num=int(e1.get())/int(e3.get())
    v.set(str(num))

def delete():
    e1.delete(0,END)
    e3.delete(0,END)
Button(root,text="加",width=10,fg="blue",command=add).grid(row=1,column=0,sticky=W,padx=10,pady=5)
Button(root,text="减",width=10,fg="blue",command=jian).grid(row=1,column=1,sticky=W,padx=10,pady=5)
Button(root,text="乘",width=10,fg="blue",command=chen).grid(row=2,column=0,sticky=W,padx=10,pady=5)
Button(root,text="除",width=10,fg="blue",command=chu).grid(row=2,column=1,sticky=W,padx=10,pady=5)
Button(root,text="计算",width=10,fg="blue",command=ji).grid(row=1,column=3,sticky=W,padx=10,pady=5)
Button(root,text="复位",fg="blue",width=10,command=delete).grid(row=3,column=2,sticky=W,padx=10,pady=5)
Button(root,text="退出",fg="blue",width=10,command=root.quit).grid(row=3,column=3,sticky=E,padx=10,pady=5)

mainloop()

猜你喜欢

转载自blog.csdn.net/qq_34717531/article/details/122962344