from tkinter import *
root = Tk()
root.wm_minsize(400, 160) # 设置窗口大小
ShowNumEntry = Entry(root, width=8, justify="right", font=12)
ShowNumEntry.grid(row=0, column=5)
def getInputValue(ShowNumEntry, Value):
ShowNumEntry.insert(END, Value)
def GetResult():
result = eval(ShowNumEntry.get())
getClean()
ShowNumEntry.insert(0, str(result))
def num_choice(num):
if num == 1:
getInputValue(ShowNumEntry,"1")
elif num==2:
getInputValue(ShowNumEntry, "2")
elif num==3:
getInputValue(ShowNumEntry, "3")
elif num==4:
getInputValue(ShowNumEntry, "4")
elif num==5:
getInputValue(ShowNumEntry, "5")
elif num==6:
getInputValue(ShowNumEntry, "6")
elif num==7:
getInputValue(ShowNumEntry, "7")
elif num==8:
getInputValue(ShowNumEntry, "8")
elif num==9:
getInputValue(ShowNumEntry, "9")
elif num==0:
getInputValue(ShowNumEntry, "0")
def operator_choice(oper):
if oper == "+":
getInputValue(ShowNumEntry,"+")
elif oper=="-":
getInputValue(ShowNumEntry, "-")
elif oper=="*":
getInputValue(ShowNumEntry, "*")
elif oper=="/":
getInputValue(ShowNumEntry, "/")
elif oper == ".":
getInputValue(ShowNumEntry, ".")
### 清空
def getClean():
ShowNumEntry.delete(0,END)
# def GetOne():
# GetInputValue(ShowNumEntry,"1")
#
#
# def GetTwo():
# GetInputValue(ShowNumEntry,"2")
#
#
# def GetThree():
# GetInputValue(ShowNumEntry,"3")
#
#
# def GetFour():
# GetInputValue(ShowNumEntry,"4")
#
#
# def GetFive():
# GetInputValue(ShowNumEntry,"5")
#
#
# def GetSix():
# GetInputValue(ShowNumEntry,"6")
#
#
# def GetSeven():
# GetInputValue(ShowNumEntry,"7")
#
#
# def GetEight():
# GetInputValue(ShowNumEntry,"8")
#
#
# def GetNine():
# GetInputValue(ShowNumEntry,"9")
#
#
# def GetZero():
# GetInputValue(ShowNumEntry,"0")
#
#
# # 加减乘除
# def GetPlus():
# GetInputValue(ShowNumEntry,"+")
#
#
# def GetMinus():
# GetInputValue(ShowNumEntry,"-")
#
#
# def GetMultiply():
# GetInputValue(ShowNumEntry,"*")
#
#
# def GetDivision():
# GetInputValue(ShowNumEntry,"/")
#
#
# def GetDot():
# GetInputValue(ShowNumEntry,".")
# def GetClean():
# ShowNumEntry.delete(0, END)
# 行一
NumOneBtn = Button(root,text="1",width=8,height=2,command=lambda:num_choice(1) )
NumOneBtn.grid(row=1,column=0)
NumTwoBtn = Button(root,text="2",width=8,height=2,command=lambda:num_choice(2))
NumTwoBtn.grid(row=1,column=1)
NumThreeBtn = Button(root,text="3",width=8,height=2,command=lambda:num_choice(3))
NumThreeBtn.grid(row=1,column=2)
PlusBtn = Button(root,text="+",width=8,height=2,command=lambda:operator_choice("+"))
PlusBtn.grid(row=1,column=3)
MinusBtn = Button(root,text="-",width=8,height=2,command=lambda:operator_choice("-"))
MinusBtn.grid(row=1,column=4)
CleanBtn = Button(root,text="Clean",width=8,height=2,command=getClean)
CleanBtn.grid(row=1,column=5)
#第三行
NumFourBtn = Button(root,text="4",width=8,height=2,command=lambda:num_choice(4))
NumFourBtn.grid(row=2,column=0)
NumFiveBtn = Button(root,text="5",width=8,height=2,command=lambda:num_choice(5))
NumFiveBtn.grid(row=2,column=1)
NumSixBtn = Button(root,text="6",width=8,height=2,command=lambda:num_choice(6))
NumSixBtn.grid(row=2,column=2)
DotBtn = Button(root,text=".",width=8,height=2,command=lambda:operator_choice("."))
DotBtn.grid(row=2,column=3)
MultiplyBtn = Button(root,text="*",width=8,height=2,command=lambda:operator_choice("*"))
MultiplyBtn.grid(row=2,column=4)
ResultBtn = Button(root,text="=",width=8,height=2,background='green',command=GetResult)
ResultBtn.grid(row=2,column=5)
#第四行
NumSevenBtn = Button(root,text="7",width=8,height=2,command=lambda:num_choice(7))
NumSevenBtn.grid(row=3,column=0)
NumEightBtn = Button(root,text="8",width=8,height=2,command=lambda:num_choice(8))
NumEightBtn.grid(row=3,column=1)
NumNineBtn = Button(root,text="9",width=8,height=2,command=lambda:num_choice(9))
NumNineBtn.grid(row=3,column=2)
NumZeroBtn = Button(root,text="0",width=8,height=2,command=lambda:num_choice(0))
NumZeroBtn.grid(row=3,column=3)
DivisionBtn = Button(root,text="/",width=8,height=2,command=lambda:operator_choice("/"))
DivisionBtn.grid(row=3,column=4)
# # 行二
# NumFourBtn = Button(root, text="4", width=8, height=2)
# NumFourBtn.grid(row=2, column=0)
#
# NumFiveBtn = Button(root, text="5", width=8, height=2)
# NumFiveBtn.grid(row=2, column=1)
#
# NumSixBtn = Button(root, text="3", width=8, height=2)
# NumSixBtn.grid(row=2, column=2)
#
# DotBtn = Button(root, text=".", width=8, height=2)
# DotBtn.grid(row=2, column=3)
#
# MultiplyBtn = Button(root, text="X", width=8, height=2)
# MultiplyBtn.grid(row=2, column=4)
# EqualBtn = Button(root, text="=", width=8, height=2)
# EqualBtn.grid(row=2, column=5)
#
# # 行三
# NumSevenBtn = Button(root, text="7", width=8, height=2)
# NumSevenBtn.grid(row=3, column=0)
#
# NumEightBtn = Button(root, text="8", width=8, height=2)
# NumEightBtn.grid(row=3, column=1)
#
# NumNineBtn = Button(root, text="9", width=8, height=2)
# NumNineBtn.grid(row=3, column=2)
#
# ZeroBtn = Button(root, text="0", width=8, height=2)
# ZeroBtn.grid(row=3, column=3)
#
# DivBtn = Button(root, text="÷", width=8, height=2)
# DivBtn.grid(row=3, column=4)
root.mainloop()