from tkinter import *
window = Tk(className="陕西理工大学")
window.geometry('320x395')
show = StringVar()
show0 = StringVar()
entry = Entry(window, justify=RIGHT, relief=RAISED, textvariable=show, font=("楷体", 20)).place(x=0, y=35, width=320, height=35)
temp = Entry(window, justify=RIGHT, textvariable=show0, font=("楷体", 16)).place(x=0, y=0, width=320, height=35)
show.set('0')
sign, A= '', ''
def number(num):
if show.get() == '0':
show.set(num)
else :
show.set(show.get()+num)
def clearAll():
show.set('0')
show0.set('')
def delone():
if show.get() != '0':
show.set(show.get()[0:-1])
def xs():
show.set(show.get()+'.')
def operator(op):
global sign, A
A = show.get()
if op=='+':
sign = '+'
show0.set(show.get()+'+')
elif op=='-':
sign = '-'
show0.set(show.get()+'-')
elif op=='*':
sign = '*'
show0.set(show.get()+'*')
elif op=='/':
sign = '/'
show0.set(show.get()+'/')
show.set('')
def rev():
if show.get() != '0':
show.set('-'+show.get())
def value(a):
global sign, A
if a:
show0.set(show.get() + '² = ')
show.set(str(eval(show.get()) ** 2))
else:
try:
show0.set(show0.get()+show.get()+' =')
show.set(round(eval(A + ' ' + sign + ' ' + show.get()), 22))
except ZeroDivisionError:
show.set("不可以除0")
x = Button(window, command=lambda: value(1), text="x²", bg='SkyBlue', font=("黑体", 20)).place(x=2, y=72, width=75, height=60)
delete = Button(window, command=lambda: delone(), text="del", fg='Red', bg='SkyBlue', font=("黑体", 20)).place(x=82, y=72, width=75, height=60)
cancer = Button(window, command=lambda: clearAll(), text="C", fg='Red', bg='SkyBlue', font=("黑体", 20)).place(x=162, y=72, width=75, height=60)
div = Button(window, command=lambda: operator('/'), text="÷", bg='SkyBlue', font=("黑体", 20)).place(x=242, y=72, width=75, height=60)
button7 = Button(window, command=lambda: number('7'), text="7", bg='Pink', font=("黑体", 20)).place(x=2, y=137, width=75, height=60)
button8 = Button(window, command=lambda: number('8'), text="8", bg='Pink', font=("黑体", 20)).place(x=82, y=137, width=75, height=60)
button9 = Button(window, command=lambda: number('9'), text="9", bg='Pink', font=("黑体", 20)).place(x=162, y=137, width=75, height=60)
mul = Button(window, command=lambda: operator('*'), text="*", bg='SkyBlue', font=("黑体", 20)).place(x=242, y=137, width=75, height=60)
button4 = Button(window, command=lambda: number('4'), text="4", bg='Pink', font=("黑体", 20)).place(x=2, y=202, width=75, height=60)
button5 = Button(window, command=lambda: number('5'), text="5", bg='Pink', font=("黑体", 20)).place(x=82, y=202, width=75, height=60)
button6 = Button(window, command=lambda: number('6'), text="6", bg='Pink', font=("黑体", 20)).place(x=162, y=202, width=75, height=60)
sub = Button(window, command=lambda: operator('-'), text="-", bg='SkyBlue', font=("黑体", 20)).place(x=242, y=202, width=75, height=60)
button1 = Button(window, command=lambda: number('1'), text="1", bg='Pink', font=("黑体", 20)).place(x=2, y=267, width=75, height=60)
button2 = Button(window, command=lambda: number('2'), text="2", bg='Pink', font=("黑体", 20)).place(x=82, y=267, width=75, height=60)
button3 = Button(window, command=lambda: number('3'), text="3", bg='Pink', font=("黑体", 20)).place(x=162, y=267, width=75, height=60)
add = Button(window, command=lambda: operator('+'), text="+", bg='SkyBlue', font=("黑体", 20)).place(x=242, y=267, width=75, height=60)
zf = Button(window, command=lambda: rev(), text="+/-", bg='Pink', font=("黑体", 16)).place(x=2, y=332, width=75, height=60)
button0 = Button(window, command=lambda: number('0'), text="0", bg='Pink', font=("黑体", 20)).place(x=82, y=332, width=75, height=60)
dian = Button(window, command=lambda: xs(), text=".", bg='Pink', fg='Red', font=("黑体", 20)).place(x=162, y=332, width=75, height=60)
dy = Button(window, command=lambda: value(0), text="=", bg='Yellow', font=("黑体", 20)).place(x=242, y=332, width=75, height=60)
window.mainloop()