用户:
沉著的飞叶龙uZWJ查看:6 回复:8 评论:6 创建时间:2020-04-16T17:04:48
【作品展示】

【作品介绍】
又肝了一会肝出了1.9版本。增加了乘方功能,在equal函数里又一点点小小的修改(自己找),增加了backspace的方法,增加了括号。
大家觉得怎么样呢?
<1.10更新预告>
目前作者仍然在纠结与是使用eval函数作为计算核心还是自己的算法作为计算核心。先跟大家说明一下,以eval作为核心的话可以支持带括号的运算、先乘方再乘除最后加减,但不能进行三角函数运算以及带入特殊无理数的值(如pi,e);使用我的算法只能从左往右算,但是可以代入pi、e两个特殊无理数,支持阶乘运算,支持三角函数运算,支持快速开平方根(即√,而非^0.5)
【作品源代码】
import math
import tkinter
import easygui#准备
calculator=tkinter.Tk()#创建窗口
calculator.geometry('400x400')#设置大小
calculator.resizable(0,0)#不许拉动
calculator.title('计算器')#设置标题
content=tkinter.StringVar()
LEDboard=tkinter.Label(calculator,textvariable=content,bg='#efefef',width=50,height=1)#假装是LED板
LEDboard.place(x=20,y=100)#放置LED板
def one():
content.set(content.get()+'1')
b1 = tkinter.Button(calculator,text='1',bg='white',width=5,height=2,command=one)
b1.place(x=100,y=200)
def two():
content.set(content.get()+'2')
b2 = tkinter.Button(calculator, text='2', bg='white',
width=5, height=2, command=two)
b2.place(x=150, y=200)
def three():
content.set(content.get()+'3')
b3 = tkinter.Button(calculator, text='3', bg='white',
width=5, height=2, command=three)
b3.place(x=200, y=200)
def four():
content.set(content.get()+'4')
b4 = tkinter.Button(calculator, text='4', bg='white',
width=5, height=2, command=four)
b4.place(x=100, y=250)
def five():
content.set(content.get()+'5')
b5 = tkinter.Button(calculator, text='5', bg='white',
width=5, height=2, command=five)
b5.place(x=150, y=250)
def six():
content.set(content.get()+'6')
b6 = tkinter.Button(calculator, text='6', bg='white',
width=5, height=2, command=six)
b6.place(x=200, y=250)
def seven():
content.set(content.get()+'7')
b7 = tkinter.Button(calculator, text='7', bg='white',
width=5, height=2, command=seven)
b7.place(x=100, y=300)
def eight():
content.set(content.get()+'8')
b8 = tkinter.Button(calculator, text='8', bg='white',
width=5, height=2, command=eight)
b8.place(x=150, y=300)
def nine():
content.set(content.get()+'9')
b9 = tkinter.Button(calculator, text='9', bg='white',
width=5, height=2, command=nine)
b9.place(x=200, y=300)
def zero():
content.set(content.get()+'0')
b0 = tkinter.Button(calculator, text='0', bg='white',
width=5, height=2, command=zero)
b0.place(x=100, y=350)
def dot():
content.set(content.get()+'.')
dian = tkinter.Button(calculator, text='.', bg='white',
width=5, height=2, command=dot)
dian.place(x=150, y=350)
'''
10个数字和点,按钮大小和位置是作者亲测的,请勿修改哦~
'''
def add():
content.set(content.get()+'+')
plus = tkinter.Button(calculator, text='+', bg='white', width=5, height=2,command=add)
plus.place(x=250,y=200)
def subtract():
content.set(content.get()+'-')
minus= tkinter.Button(calculator, text='-', bg='white',
width=5, height=2, command=subtract)
minus.place(x=250, y=250)
def multiple():
content.set(content.get()+'×')
times= tkinter.Button(calculator, text='×', bg='white',
width=5, height=2, command=multiple)
times.place(x=300, y=250)
def divide():
content.set(content.get()+'÷')
chu= tkinter.Button(calculator, text='÷', bg='white',
width=5, height=2, command=divide)
chu.place(x=250, y=350)
def equal():
content.set(content.get()+'=')
try:
content.set(content.get(
)+str(eval(content.get().replace('×', '*').replace('÷', '/').replace('^','**').split('=')[0])))
easygui.msgbox('喵进行下一次计算', '一个极其普通的计算器', '喵')
content.set('')
except:
easygui.msgbox('输入有误!')
content.set('')
dengyu= tkinter.Button(calculator, text='=', bg='white',
width=5, height=2, command=equal)
dengyu.place(x=200, y=350)
def chengfang():
content.set(content.get()+'^')
mi= tkinter.Button(calculator, text='^', bg='white',
width=5, height=2, command=chengfang)
mi.place(x=300, y=200)
def zuokuohao():
content.set(content.get()+'(')
leftkuohao= tkinter.Button(calculator, text='(', bg='white',
width=5, height=2, command=zuokuohao)
leftkuohao.place(x=250,y=300)
def youkuohao():
content.set(content.get()+')')
rightkuohao= tkinter.Button(calculator, text=')', bg='white',
width=5, height=2, command=youkuohao)
rightkuohao.place(x=300,y=300)
def backspace():
content.set(''.join(list(content.get()[0:len(content.get())-1])))
huishan=tkinter.Button(calculator, text='<-|x|', bg='white',
width=5, height=2, command=backspace)
huishan.place(x=300,y=350)
calculator.mainloop() # 运行计算器
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cnpi和e和sin/cos/tan可以:
s=content.replace('e','math.e').replace('π','math.pi')……#然后直接用eval点赞1
评论