源代码:
import tkinter
tk=tkinter.Tk()
tk.geometry('300x210+500+200')
tk.resizable(False,False)
tk.title('计算器')
contentVar=tkinter.StringVar(tk,'')
contentEntry=tkinter.Entry(tk,textvariable=contentVar)
contentEntry['state']='readonly'
contentEntry.place(x=20,y=10,width=260,height=30)
bvalue=['C','+','-','//','7','8','9','✓','4','5','6','*','1','2','3','.','0','/','**','=']
index=0
for row in range(5):
for col in range(4):
d=bvalue[index]
index+=1
btnDight=tkinter.Button(tk,text=d,command=lambda x=d:onclick(x))
btnDight.place(x=20+col*70,y=50+row*30,width=50,height=20)
def onclick(btn):
operation=('+','-','*','/','**','//')
content=contentVar.get()
if content.startswith('.'):
content='0'+content
if btn in '0123456789':
content+=btn
elif btn=='.':
lastPart=re.split(r'\+|-|\*|/',content)[-1]
if '.' in lastPart:
tkinter.messagebox.showerror('错误','重复出现的小数点')
return
else:
content+=btn
elif btn=='C':
content=''
elif btn=='=':
try:
content=str(eval(content))
except:
tkinter.messagebox.showerror('错误','表达式有误')
return
elif btn in operation:
if content.endswith(operation):
tkinter.messagebox.showerror('错误','不允许存在连续运算符')
return
content+=btn
elif btn=='✓':
n=content.spilt('.')
if all(map(lambda x:x.isdight(),n)):
content=eval(content)**0.5
else:
tkinter.messagebox.showerror('错误','表达式错误')
return
contentVar.set(content)
这个代码主要是运用tkinter窗口以及运用def语句定义按键,用if语句假设条件。