这是一个简易的GUI计算器,可以让大家进行简单的科学计算,代码如下:
import tkinter as tk
import math
def exp(x):
return math.exp(x)
log=math.log
def log10(x):
return math.log10(x)
def sqrt(x):
return math.sqrt(x)
def acos(x):
return math.acos(x)
def asin(x):
return math.asin(x)
def atan(x):
return math.atan(x)
def cos(x):
return math.cos(x)
def sin(x):
return math.sin(x)
def tan(x):
return math.tan(x)
def acosh(x):
return math.acosh(x)
def asinh(x):
return math.asinh(x)
def atanh(x):
return math.atanh(x)
def cosh(x):
return math.cosh(x)
def sinh(x):
return math.sinh(x)
def tanh(x):
return math.tanh(x)
pi=math.pi
e=math.e
from random import randint
print=lambda *arg:None
string=['\0','\0']
pos=1
i=''
mainw=tk.Tk() #主窗口
mainw.title("计算器")
mainw.resizable(False, False)
mainw.geometry('600x600')
result=tk.Label(mainw, bg='grey',font=('Arial',20),height=1)
result.place(x=0, y=70,relwidth=1)
equation=tk.Label(mainw, bg='grey',font=('Arial',20),height=1)
equation.place(x=0, y=35,relwidth=1)
def addstr(i,s):
global string,pos,mainw,equation
string.insert(pos,s)
print(string,pos,pos+1)
pos+=1
s=''
for i in range(1,len(string)-1):
s+=string[i]
rs=tk.StringVar()
rs.set(s)
equation.destroy()
equation=tk.Label(mainw, bg='grey',text=rs.get(),font=('Arial',20),width=30,height=1)
equation.place(x=0,y=35,relwidth=1)
mainw.update()
def addi():
global string,pos,i
print(string)
string.insert(pos,i)
pos+=1
def equal_func():
global string,mainw,result
if len(string)!=2:
s=''
for i in range(1,len(string)-1):
s+=string[i]
rs=tk.StringVar()
try:
rs.set('='+str(eval(s)))
except ZeroDivisionError:
print("除零错误")
rs.set("除零错误")
except TypeError:
print("参数过多/类型错误")
rs.set("参数过多/类型错误")
except SyntaxError:
print("语法错误")
rs.set("语法错误")
except ValueError:
print("数值过大/类型错误")
rs.set("数值过大/类型错误")
except Exception as e:
print("发生了一个未知错误:", e)
rs.set("发生了一个未知错误:"+e)
#else:
# rs.set('='+str(eval(s)))
result.destroy()
result=tk.Label(mainw, bg='grey',text=rs.get(),font=('Arial',20),width=30,height=1)
result.place(x=0,y=70,relwidth=1)
mainw.update()
def AC():
global string,pos,equation,result
string=['\0','\0']
pos=1
print(string,pos)
rs=tk.StringVar()
rs.set('')
equation.destroy()
equation=tk.Label(mainw, bg='grey',text=rs.get(),font=('Arial',20),width=30,height=1)
equation.place(x=0,y=35,relwidth=1)
result.destroy()
result=tk.Label(mainw, bg='grey',text=rs.get(),font=('Arial',20),width=30,height=1)
result.place(x=0,y=70,relwidth=1)
mainw.update()
def delete():
global pos,equation
if len(string)>2 and pos!=1:
del string[pos-1]
pos-=1
print(string,pos)
s=''
for i in range(1,len(string)-1):
s+=string[i]
rs=tk.StringVar()
rs.set(s)
equation.destroy()
equation=tk.Label(mainw, bg='grey',text=rs.get(),font=('Arial',20),width=30,height=1)
equation.place(x=0,y=35,relwidth=1)
mainw.update()
def move_left():
global pos
if pos>1:
pos-=1
elif pos==2:
pos=len(string)-1
print(pos)
def move_right():
global pos
if pos<len(string)-1:
pos+=1
elif pos==len(string)-1 and len(string)!=2:
pos=1
print(pos)
buttondot = tk.Button(mainw, text='.', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'.'))
buttondot.place(x=100,y=500)
button0 = tk.Button(mainw, text='0', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'0'))
button0.place(x=200,y=500)
buttonrandint = tk.Button(mainw, text='RandInt(', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'randint('))
buttonrandint.place(x=300,y=500)
button1 = tk.Button(mainw, text='1', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'1'))
button1.place(x=100,y=470)
button2 = tk.Button(mainw, text='2', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'2'))
button2.place(x=200,y=470)
button3 = tk.Button(mainw, text='3', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'3'))
button3.place(x=300,y=470)
button4 = tk.Button(mainw, text='4', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'4'))
button4.place(x=100,y=440)
button5 = tk.Button(mainw, text='5', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'5'))
button5.place(x=200,y=440)
button6 = tk.Button(mainw, text='6', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'6'))
button6.place(x=300,y=440)
button7 = tk.Button(mainw, text='7', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'7'))
button7.place(x=100,y=410)
button8 = tk.Button(mainw, text='8', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'8'))
button8.place(x=200,y=410)
button9 = tk.Button(mainw, text='9', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'9'))
button9.place(x=300,y=410)
buttondel=tk.Button(mainw, text='del', font=('Arial', 12), width=10, height=1, command=delete)
buttondel.place(x=400,y=380)
buttonbracketsr = tk.Button(mainw, text=')', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,')'))
buttonbracketsr.place(x=300,y=380)
buttonbracketsl = tk.Button(mainw, text='(', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'('))
buttonbracketsl.place(x=200,y=380)
button_l=tk.Button(mainw, text='←', font=('Arial', 12), width=10, height=1, command=move_left)
button_l.place(x=150,y=0)
button_r=tk.Button(mainw, text='→', font=('Arial', 12), width=10, height=1, command=move_right)
button_r.place(x=250,y=0)
buttonadd = tk.Button(mainw, text='+', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'+'))
buttonadd.place(x=400,y=410)
buttonsub = tk.Button(mainw, text='-', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'-'))
buttonsub.place(x=400,y=440)
buttonmul = tk.Button(mainw, text='×', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'*'))
buttonmul.place(x=400,y=470)
buttondiv = tk.Button(mainw, text='÷', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'/'))
buttondiv.place(x=400,y=500)
buttonpow = tk.Button(mainw, text='xʸ', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'**'))
buttonpow.place(x=100,y=380)
buttonequal = tk.Button(mainw, text='=', font=('Arial', 12), width=10, height=1, command=equal_func)
buttonequal.place(x=500,y=500)
#数学函数
buttonexp = tk.Button(mainw, text='eˣ', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'exp('))
buttonexp.place(x=100,y=320)
buttonlog = tk.Button(mainw, text='log(', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'log('))
buttonlog.place(x=200,y=320)
buttonlog10 = tk.Button(mainw, text='log₁₀(', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'log10('))
buttonlog10.place(x=300,y=320)
buttonsqrt = tk.Button(mainw, text='√⁻(', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'sqrt('))
buttonsqrt.place(x=400,y=320)
buttonacos = tk.Button(mainw, text='cos⁻¹(', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'acos('))
buttonacos.place(x=200,y=260)
buttonasin = tk.Button(mainw, text='sin⁻¹(', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'asin('))
buttonasin.place(x=100,y=260)
buttonatan = tk.Button(mainw, text='tan⁻¹(', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'atan('))
buttonatan.place(x=300,y=260)
buttoncos = tk.Button(mainw, text='cos(', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'cos('))
buttoncos.place(x=200,y=290)
buttonsin = tk.Button(mainw, text='sin(', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'sin('))
buttonsin.place(x=100,y=290)
buttontan = tk.Button(mainw, text='tan(', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'tan('))
buttontan.place(x=300,y=290)
buttonacosh = tk.Button(mainw, text='acosh(', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'acosh('))
buttonacosh.place(x=200,y=200)
buttonasinh = tk.Button(mainw, text='asinh(', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'asinh('))
buttonasinh.place(x=100,y=200)
buttonatanh = tk.Button(mainw, text='atanh(', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'atanh('))
buttonatanh.place(x=300,y=200)
buttoncosh = tk.Button(mainw, text='cosh(', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'cosh('))
buttoncosh.place(x=200,y=230)
buttonsinh = tk.Button(mainw, text='sinh(', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'sinh('))
buttonsinh.place(x=100,y=230)
buttontanh = tk.Button(mainw, text='tanh', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'tanh('))
buttontanh.place(x=300,y=230)
buttonpi = tk.Button(mainw, text='π', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'pi'))
buttonpi.place(x=400,y=260)
buttone = tk.Button(mainw, text='e', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,'e'))
buttone.place(x=400,y=290)
buttone = tk.Button(mainw, text=',', font=('Arial', 12), width=10, height=1, command=lambda:addstr(pos,','))
buttone.place(x=400,y=380)
ACbutton = tk.Button(mainw, text='AC', font=('Arial', 12), width=10, height=1, command=AC)
ACbutton.place(x=0,y=0)
mainw.mainloop()
没有安装python的可以直接运行打包后的程序(会被杀毒软件误判):