eval语句,具有自动计算功能
借助tkinter完成图形界面描述
流程如下:
from tkinter import *
win=Tk()
win.title('计算器')
win.geometry('450x680')
2.放置按钮:
but7=Button(win,text=7,width=3,font=('Arial',31),command=lambda :numinput(7))#如果只是先添加命令可以先不添加command关键字
but8=Button(win,text=8,width=3,font=('Arial',31),command=lambda :numinput(8))
but9=Button(win,text=9,width=3,font=('Arial',31),command=lambda :numinput(9))
but4=Button(win,text=4,width=3,font=('Arial',31),command=lambda :numinput(4))
but5=Button(win,text=5,width=3,font=('Arial',31),command=lambda :numinput(5))
but6=Button(win,text=6,width=3,font=('Arial',31),command=lambda :numinput(6))
but1=Button(win,text=1,width=3,font=('Arial',31),command=lambda :numinput(1))
but2=Button(win,text=2,width=3,font=('Arial',31),command=lambda :numinput(2))
but3=Button(win,text=3,width=3,font=('Arial',31),command=lambda :numinput(3))
but0=Button(win,text=0,width=3,font=('Arial',31),command=lambda :numinput(0))
bu1=Button(win,text='ans',width=3,font=('Arial',31),command=ans_)
bu2=Button(win,text='π',width=3,font=('Arial',31),command=lambda :numinput(pi))
bu3=Button(win,text='e',width=3,font=('Arial',31),command=lambda :numinput(e))
bu4=Button(win,text='CE',width=3,font=('Arial',31),command=clear)
bu5=Button(win,text='del',width=3,font=('Arial',31),command=delete_n)
bu6=Button(win,text='x^2',width=3,font=('Arial',31),command=mi_2)
bu7=Button(win,text='1/x',width=3,font=('Arial',31),command=x_1)
bu8=Button(win,text='|x|',width=3,font=('Arial',31),command=juedui)
bu9=Button(win,text='exp',width=3,font=('Arial',31),command=lambda :add_char('exp'))
bu0=Button(win,text='mod',width=3,font=('Arial',31),command=lambda:add_char('%'))
b1=Button(win,text='sqrt()',width=3,font=('Arial',31),command=sqrt_)
b2=Button(win,text='(',width=3,font=('Arial',31),command=lambda :add_char('('))
b3=Button(win,text=')',width=3,font=('Arial',31),command=lambda :add_char(')'))
b4=Button(win,text='n!',width=3,font=('Arial',31),command=jiechen)
b5=Button(win,text='/',width=3,font=('Arial',31),command=lambda :add_char('/'))
b6=Button(win,text='x^y',width=3,font=('Arial',31),command=lambda :add_char('^'))
b7=Button(win,text='*',width=3,font=('Arial',31),command=lambda :add_char('*'))
b8=Button(win,text='10^x',width=3,font=('Arial',31),command=zhi_10)
b9=Button(win,text='-',width=3,font=('Arial',31),command=lambda :add_char('-'))
b0=Button(win,text='log',width=3,font=('Arial',31),command=log_)
b11=Button(win,text='+',width=3,font=('Arial',31),command=lambda :add_char('+'))
b12=Button(win,text='ln',width=3,font=('Arial',31),command=IN_)
b13=Button(win,text='+/-',width=3,font=('Arial',31),command=fu)
b14=Button(win,text='.',width=3,font=('Arial',31),command=lambda :add_char('.'))
b15=Button(win,text='=',width=3,font=('Arial',31),command=end1)
b6.place(x=0,y=320)
but7.place(x=90,y=320)
but8.place(x=180,y=320)
but9.place(x=270,y=320)
b7.place(x=360,y=320)
but4.place(x=90,y=410)
but5.place(x=180,y=410)
but6.place(x=270,y=410)
b8.place(x=0,y=410)
b9.place(x=360,y=410)
but1.place(x=90,y=500)
but2.place(x=180,y=500)
but3.place(x=270,y=500)
b0.place(x=0,y=500)
b11.place(x=360,y=500)
b13.place(x=90,y=590)
but0.place(x=180,y=590)
b14.place(x=270,y=590)
b12.place(x=0,y=590)
b15.place(x=360,y=590)
bu2.place(x=90,y=50)
bu3.place(x=180,y=50)
bu4.place(x=270,y=50)
bu1.place(x=0,y=50)
bu5.place(x=360,y=50)
bu7.place(x=90,y=140)
bu8.place(x=180,y=140)
bu9.place(x=270,y=140)
bu6.place(x=0,y=140)
bu0.place(x=360,y=140)
b2.place(x=90,y=230)
b3.place(x=180,y=230)
b4.place(x=270,y=230)
b1.place(x=0,y=230)
b5.place(x=360,y=230)
en=Entry(win,width=30,font=('Arial',20))
en.place(x=0,y=0)
ans=0.0
flag=0
falg=0
def er_yuan():
num=str(en.get())
for i in range(0,len(num)):
if (str.isdigit(num[i]) or num[i] == '.') == 0:
number=eval(num[0:i])
print(number)
en.delete(0,END)
if num[i]=='^':
en.insert(0,str(number**(eval(num[i+1:]))))
else:
en.insert(0,str(number*(10**eval(num[i+3:]))))
return
def numinput(number):
global flag
if (flag):
en.delete(0,END)
flag=0
first_num=en.get()
en.delete(0,END)
en.insert(0,str(first_num)+str(number))
def clear():
en.delete(0,END)
def delete_n():
en.delete(len(str(en.get()))-1,END)
def x_1():
num=str(en.get())
n=1
# if(num[-1]==')'):#如果想要整体做1/x可以使用加括号的方法
# en.delete(0,END)
# en.insert(0,str(1/eval(num[1:-2])))
# return
if(num[-1]==')'):
for i in range(len(num)-2,0,-1):
if(num[i]==')'):
n+=1
elif num[i]=='(':
n-=1
if(n==0):
en.delete(0,END)
# print(num[i+1:-2])
en.insert(0,num[0:i]+str(1/eval(num[i+1:-1])))
#注意切片也是满足左开右闭的情况,只有右侧不取数字时才有
# 可以得到从对应位置到最后的结果
return
for i in range(len(num)-1,0,-1):
if (str.isdigit(num[i]) or num[i]=='.')==0:
pp=float(num[i+1:])
if pp==0:
en.delete(0,END)
en.insert('error')
print('ok')
en.delete(0,END)
en.insert(0,num[0:i+1]+str(1/pp))
return
en.delete(0,END)
en.insert(0,str(1/float(num[0:])))
def mi_2():
num=str(en.get())
for i in range(len(num)-1,0,-1):#只需要读到第二个字符即可,因为如果第二个字符都不是字符
#则即使前面是字符也是属于单目符号(这里只有 自取负符号-)对结果无关紧要
if (str.isdigit(num[i]) or num[i]=='.')==0:
en.delete(0,END)
en.insert(0,num[0:i+1]+str(eval(num[i:])*eval(num[i:])))
return
en.delete(0,END)
en.insert(0,str(eval(num)*eval(num)))
#当点了=之后如果第一个为数字则就可以重新开始若要使用之前的数据可以使用ans
# 但是如果是使用的符号则需要使用之前的数字不做修改
def add_char(x):
global falg
if (x=='exp' or x=='^'):
falg=1
num = en.get()
en.delete(0,END)
global flag
if(flag):
en.insert(0,str(ans)+x)
flag=0
else:
en.insert(0, str(num) +x)
def end1():
global ans
global flag
global falg
# for i in str(en.get()):
if falg:
er_yuan()
falg=0
return
num=eval(str(en.get()))
en.delete(0,END)
en.insert(0,str(num))
ans=num
flag=1
def ans_():
global ans
num=en.get()
en.delete(0, END)
en.insert(0, str(num)+str(ans))
def juedui():
num = str(en.get())
en.delete(0, END)
en.insert(0, str(abs(eval(num))))
def jiechen():
num = str(en.get())
en.delete(0, END)
sum=1
k=abs(eval(num))
for i in range(2,k+1,1):
sum*=i
en.insert(0, str(sum))
def sqrt_():
num = str(en.get())
for i in range(len(num) - 1, 0, -1):
if (str.isdigit(num[i]) or num[i]=='.')==0:
en.delete(0, END)
en.insert(0, num[0:i+1] + str(math.sqrt(eval(num[i+1:]))))
return
en.delete(0, END)
en.insert(0, str(math.sqrt(eval(num))))
def zhi_10():
num = str(en.get())
for i in range(len(num) - 1, 0, -1):
if (str.isdigit(num[i]) or num[i]=='.')==0:
en.delete(0, END)
en.insert(0, num[0:i+1] + str(10**(eval(num[i+1:]))))
return
en.delete(0, END)
en.insert(0, str(10**(eval(num))))
def fu():
num=en.get()
en.delete(0,END)
en.insert(0,'-'+str(num))
def log_():
num = str(en.get())
for i in range(len(num) - 1, 0, -1):
if (str.isdigit(num[i]) or num[i] == '.') == 0:
en.delete(0, END)
en.insert(0, num[0:i + 1] + str(math.log10(eval(num[i + 1:]))))
return
en.delete(0, END)
if eval(num[0:])<=0:
en.insert(0,'error')
return
en.insert(0, str(math.log10(eval(num[0:]))))
def IN_():
num = str(en.get())
for i in range(len(num) - 1, 0, -1):
if (str.isdigit(num[i]) or num[i] == '.') == 0:
en.delete(0, END)
en.insert(0, num[0:i + 1] + str(math.log(eval(num[i + 1:]))))
return
en.delete(0, END)
if eval(num[0:]) <= 0:
en.insert(0, 'error')
return
en.insert(0, str(math.log(eval(num[0:]))))
from tkinter import *
from math import pi
from math import e
import math
win=Tk()
win.title('计算器')
win.geometry('450x680')
ans=0.0
flag=0
falg=0
def er_yuan():
num=str(en.get())
for i in range(0,len(num)):
if (str.isdigit(num[i]) or num[i] == '.') == 0:
number=eval(num[0:i])
print(number)
en.delete(0,END)
if num[i]=='^':
en.insert(0,str(number**(eval(num[i+1:]))))
else:
en.insert(0,str(number*(10**eval(num[i+3:]))))
return
def numinput(number):
global flag
if (flag):
en.delete(0,END)
flag=0
first_num=en.get()
en.delete(0,END)
en.insert(0,str(first_num)+str(number))
def clear():
en.delete(0,END)
def delete_n():
en.delete(len(str(en.get()))-1,END)
def x_1():
num=str(en.get())
n=1
# if(num[-1]==')'):#如果想要整体做1/x可以使用加括号的方法
# en.delete(0,END)
# en.insert(0,str(1/eval(num[1:-2])))
# return
if(num[-1]==')'):
for i in range(len(num)-2,0,-1):
if(num[i]==')'):
n+=1
elif num[i]=='(':
n-=1
if(n==0):
en.delete(0,END)
# print(num[i+1:-2])
en.insert(0,num[0:i]+str(1/eval(num[i+1:-1])))
#注意切片也是满足左开右闭的情况,只有右侧不取数字时才有
# 可以得到从对应位置到最后的结果
return
for i in range(len(num)-1,0,-1):
if (str.isdigit(num[i]) or num[i]=='.')==0:
pp=float(num[i+1:])
if pp==0:
en.delete(0,END)
en.insert('error')
print('ok')
en.delete(0,END)
en.insert(0,num[0:i+1]+str(1/pp))
return
en.delete(0,END)
en.insert(0,str(1/float(num[0:])))
def mi_2():
num=str(en.get())
for i in range(len(num)-1,0,-1):#只需要读到第二个字符即可,因为如果第二个字符都不是字符
#则即使前面是字符也是属于单目符号(这里只有 自取负符号-)对结果无关紧要
if (str.isdigit(num[i]) or num[i]=='.')==0:
en.delete(0,END)
en.insert(0,num[0:i+1]+str(eval(num[i:])*eval(num[i:])))
return
en.delete(0,END)
en.insert(0,str(eval(num)*eval(num)))
#当点了=之后如果第一个为数字则就可以重新开始若要使用之前的数据可以使用ans
# 但是如果是使用的符号则需要使用之前的数字不做修改
def add_char(x):
global falg
if (x=='exp' or x=='^'):
falg=1
num = en.get()
en.delete(0,END)
global flag
if(flag):
en.insert(0,str(ans)+x)
flag=0
else:
en.insert(0, str(num) +x)
def end1():
global ans
global flag
global falg
# for i in str(en.get()):
if falg:
er_yuan()
falg=0
return
num=eval(str(en.get()))
en.delete(0,END)
en.insert(0,str(num))
ans=num
flag=1
def ans_():
global ans
num=en.get()
en.delete(0, END)
en.insert(0, str(num)+str(ans))
def juedui():
num = str(en.get())
en.delete(0, END)
en.insert(0, str(abs(eval(num))))
def jiechen():
num = str(en.get())
en.delete(0, END)
sum=1
k=abs(eval(num))
for i in range(2,k+1,1):
sum*=i
en.insert(0, str(sum))
def sqrt_():
num = str(en.get())
for i in range(len(num) - 1, 0, -1):
if (str.isdigit(num[i]) or num[i]=='.')==0:
en.delete(0, END)
en.insert(0, num[0:i+1] + str(math.sqrt(eval(num[i+1:]))))
return
en.delete(0, END)
en.insert(0, str(math.sqrt(eval(num))))
def zhi_10():
num = str(en.get())
for i in range(len(num) - 1, 0, -1):
if (str.isdigit(num[i]) or num[i]=='.')==0:
en.delete(0, END)
en.insert(0, num[0:i+1] + str(10**(eval(num[i+1:]))))
return
en.delete(0, END)
en.insert(0, str(10**(eval(num))))
def fu():
num=en.get()
en.delete(0,END)
en.insert(0,'-'+str(num))
def log_():
num = str(en.get())
for i in range(len(num) - 1, 0, -1):
if (str.isdigit(num[i]) or num[i] == '.') == 0:
en.delete(0, END)
en.insert(0, num[0:i + 1] + str(math.log10(eval(num[i + 1:]))))
return
en.delete(0, END)
if eval(num[0:])<=0:
en.insert(0,'error')
return
en.insert(0, str(math.log10(eval(num[0:]))))
def IN_():
num = str(en.get())
for i in range(len(num) - 1, 0, -1):
if (str.isdigit(num[i]) or num[i] == '.') == 0:
en.delete(0, END)
en.insert(0, num[0:i + 1] + str(math.log(eval(num[i + 1:]))))
return
en.delete(0, END)
if eval(num[0:]) <= 0:
en.insert(0, 'error')
return
en.insert(0, str(math.log(eval(num[0:]))))
but7=Button(win,text=7,width=3,font=('Arial',31),command=lambda :numinput(7))
but8=Button(win,text=8,width=3,font=('Arial',31),command=lambda :numinput(8))
but9=Button(win,text=9,width=3,font=('Arial',31),command=lambda :numinput(9))
but4=Button(win,text=4,width=3,font=('Arial',31),command=lambda :numinput(4))
but5=Button(win,text=5,width=3,font=('Arial',31),command=lambda :numinput(5))
but6=Button(win,text=6,width=3,font=('Arial',31),command=lambda :numinput(6))
but1=Button(win,text=1,width=3,font=('Arial',31),command=lambda :numinput(1))
but2=Button(win,text=2,width=3,font=('Arial',31),command=lambda :numinput(2))
but3=Button(win,text=3,width=3,font=('Arial',31),command=lambda :numinput(3))
but0=Button(win,text=0,width=3,font=('Arial',31),command=lambda :numinput(0))
bu1=Button(win,text='ans',width=3,font=('Arial',31),command=ans_)
bu2=Button(win,text='π',width=3,font=('Arial',31),command=lambda :numinput(pi))
bu3=Button(win,text='e',width=3,font=('Arial',31),command=lambda :numinput(e))
bu4=Button(win,text='CE',width=3,font=('Arial',31),command=clear)
bu5=Button(win,text='del',width=3,font=('Arial',31),command=delete_n)
bu6=Button(win,text='x^2',width=3,font=('Arial',31),command=mi_2)
bu7=Button(win,text='1/x',width=3,font=('Arial',31),command=x_1)
bu8=Button(win,text='|x|',width=3,font=('Arial',31),command=juedui)
bu9=Button(win,text='exp',width=3,font=('Arial',31),command=lambda :add_char('exp'))
bu0=Button(win,text='mod',width=3,font=('Arial',31),command=lambda:add_char('%'))
b1=Button(win,text='sqrt()',width=3,font=('Arial',31),command=sqrt_)
b2=Button(win,text='(',width=3,font=('Arial',31),command=lambda :add_char('('))
b3=Button(win,text=')',width=3,font=('Arial',31),command=lambda :add_char(')'))
b4=Button(win,text='n!',width=3,font=('Arial',31),command=jiechen)
b5=Button(win,text='/',width=3,font=('Arial',31),command=lambda :add_char('/'))
b6=Button(win,text='x^y',width=3,font=('Arial',31),command=lambda :add_char('^'))
b7=Button(win,text='*',width=3,font=('Arial',31),command=lambda :add_char('*'))
b8=Button(win,text='10^x',width=3,font=('Arial',31),command=zhi_10)
b9=Button(win,text='-',width=3,font=('Arial',31),command=lambda :add_char('-'))
b0=Button(win,text='log',width=3,font=('Arial',31),command=log_)
b11=Button(win,text='+',width=3,font=('Arial',31),command=lambda :add_char('+'))
b12=Button(win,text='ln',width=3,font=('Arial',31),command=IN_)
b13=Button(win,text='+/-',width=3,font=('Arial',31),command=fu)
b14=Button(win,text='.',width=3,font=('Arial',31),command=lambda :add_char('.'))
b15=Button(win,text='=',width=3,font=('Arial',31),command=end1)
b6.place(x=0,y=320)
but7.place(x=90,y=320)
but8.place(x=180,y=320)
but9.place(x=270,y=320)
b7.place(x=360,y=320)
but4.place(x=90,y=410)
but5.place(x=180,y=410)
but6.place(x=270,y=410)
b8.place(x=0,y=410)
b9.place(x=360,y=410)
but1.place(x=90,y=500)
but2.place(x=180,y=500)
but3.place(x=270,y=500)
b0.place(x=0,y=500)
b11.place(x=360,y=500)
b13.place(x=90,y=590)
but0.place(x=180,y=590)
b14.place(x=270,y=590)
b12.place(x=0,y=590)
b15.place(x=360,y=590)
bu2.place(x=90,y=50)
bu3.place(x=180,y=50)
bu4.place(x=270,y=50)
bu1.place(x=0,y=50)
bu5.place(x=360,y=50)
bu7.place(x=90,y=140)
bu8.place(x=180,y=140)
bu9.place(x=270,y=140)
bu6.place(x=0,y=140)
bu0.place(x=360,y=140)
b2.place(x=90,y=230)
b3.place(x=180,y=230)
b4.place(x=270,y=230)
b1.place(x=0,y=230)
b5.place(x=360,y=230)
en=Entry(win,width=30,font=('Arial',20))
en.place(x=0,y=0)
mainloop()