用户:
沉著的飞叶龙uZWJ查看:3 回复:2 评论:3 创建时间:2020-04-20T09:15:03
【作品展示】

【作品介绍】
本来没有收到关于算法的建议,我是打算分成两条路走到。一条用eval,一条用我的算法。但是,我收到了@LAG的1048576一枚的极其巧妙的建议,决定先继续更新eval算法的计算器。
更新内容:
1、大家熟悉的"<-|x|"被我用更形象的符号替代了。
2/依靠@LAG的1048576一枚的建议,我完成了在eval算法里加入π和e。
3、加入了一键清空的方法,大家不用一个一个的删除了!
【作品源代码】
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('^', '**').replace('π',str(math.pi)).
replace('e',str(math.e)).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=chr(9003), bg='white',
width=5, height=2, command=backspace)#回删
huishan.place(x=300,y=350)
def pai():
content.set(content.get()+'π')
yuanzhoulv=tkinter.Button(calculator, text='π', bg='white',
width=5, height=2, command=pai)#圆周率
yuanzhoulv.place(x=50,y=200)
def e():
content.set(content.get()+'e')#e
E=tkinter.Button(calculator, text='e', bg='white',
width=5, height=2, command=e)
E.place(x=50,y=250)
def all_clear():
content.set('')
C=tkinter.Button(calculator,text='C',bg='white',width=5,height=2,command=all_clear)
C.place(x=0,y=200)
calculator.mainloop() # 运行计算器
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cnimport tkinter as tk #导入tkinter模块
import math as m
#创建一个跟窗口
root = tk.Tk()
root.minsize(280,470)
root.title('计算器')
#1.界面布局
#显示面板
result1 = tk.StringVar()
result1.set(0) #显示面板显示结果1,用于显示默认数字0
result2 = tk.StringVar() #显示面板显示结果2,用于显示计算过程
result2.set('')
#显示屏幕
screen2 = tk.Label(root,font = ('微软雅黑',20),bg = '#EEE9E9',bd ='9',fg = 'black',textvariable=result1,anchor='se')
screen2.place(width = 280,height = 170)
screen1 = tk.Label(root,font = ('微软雅黑',30),bg = '#EEE9E9',bd ='9',fg = 'black',textvariable=result2,anchor='se')
screen1.place(width = 280,height = 110)
#从上往下第一行
buttonmod = tk.Button(root,font = ('微软雅黑',20),text='%',bd ='0.5',fg = 'black',command=lambda : pressfunction('//'))
buttonmod.place(x=0,y=170,width=70,height=50)
buttonsqrt = tk.Button(root,font = ('微软雅黑',20),text='√',bd ='0.5',fg = 'black',command=lambda : pressfunction('√'))
buttonsqrt.place(x=70,y=170,width=70,height=50)
buttonpower = tk.Button(root,font = ('微软雅黑',20),text='x²',bd ='0.5',fg = 'black',command=lambda : pressfunction('x²'))
buttonpower.place(x=140,y=170,width=70,height=50)
buttondowncount = tk.Button(root,font=('微软雅黑',20),text='1/x',bd='0.5',fg = 'black',command=lambda : pressfunction('1/x'))
buttondowncount.place(x=210,y=170,width=70,height=50)
#第二行
buttonce = tk.Button(root,font=('微软雅黑',20),text='CE',bg='orange',bd='0.5',fg='black',command=lambda : pressfunction('CE'))
buttonce.place(x=0,y=220,width=70,height=50)
buttonc = tk.Button(root,font=('微软雅黑',20),text='C',bd='0.5',fg='black',command=lambda : pressfunction('C'))
buttonc.place(x=70,y=220,width=70,height=50)
buttondelete = tk.Button(root,font=('微软雅黑',20),text='←',bd='0.5',fg='black',command=lambda : pressfunction('←'))
buttondelete.place(x=140,y=220,width=70,height=50)
buttondiv = tk.Button(root,font=('微软雅黑',20),text='÷',bd='0.5',fg='black',command=lambda : pressfunction('/'))
buttondiv.place(x=210,y=220,width=70,height=50)
#第三行
button7 = tk.Button(root,font=('微软雅黑',20),text='7',bd='0.5',bg='white',command= lambda : pressnumber('7'))
button7.place(x=0,y=270,width=70,height=50)
button8 = tk.Button(root,font=('微软雅黑',20),text='8',bd='0.5',bg='white',command=lambda : pressnumber('8'))
button8.place(x=70,y=270,width=70,height=50)
button9 = tk.Button(root,font=('微软雅黑',20),text='9',bd='0.5',bg='white',command=lambda : pressnumber('9'))
button9.place(x=140,y=270,width=70,height=50)
buttonmul = tk.Button(root,font=('微软雅黑',20),text='×',bd='0.5',fg='black',command=lambda : pressfunction('*'))
buttonmul.place(x=210,y=270,width=70,height=50)
#第四行
button4 = tk.Button(root,font=('微软雅黑',20),text='4',bd='0.5',bg='white',command=lambda : pressnumber('4'))
button4.place(x=0,y=320,width=70,height=50)
button5 = tk.Button(root,font=('微软雅黑',20),text='5',bd='0.5',bg='white',command=lambda : pressnumber('5'))
button5.place(x=70,y=320,width=70,height=50)
button6 = tk.Button(root,font=("微软雅黑",20),text='6',bd='0.5',bg='white',command=lambda : pressnumber('6'))
button6.place(x=140,y=320,width=70,height=50)
buttonsub = tk.Button(root,font=('微软雅黑',20),text='-',bd='0.5',fg='black',command=lambda : pressfunction('-'))
buttonsub.place(x=210,y=320,width=70,height=50)
#第五行
button1 = tk.Button(root,font=('微软雅黑',20),text='1',bd='0.5',bg='white',command=lambda : pressnumber('1'))
button1.place(x=0,y=370,width=70,height=50)
button2 = tk.Button(root,font=('微软雅黑',20),text='2',bd='0.5',bg='white',command=lambda : pressnumber('2'))
button2.place(x=70,y=370,width=70,height=50)
button3 = tk.Button(root,font=("微软雅黑",20),text='3',bd='0.5',bg='white',command=lambda : pressnumber('3'))
button3.place(x=140,y=370,width=70,height=50)
buttonadd = tk.Button(root,font=('微软雅黑',20),text='+',bd='0.5',fg='black',command=lambda : pressfunction('+'))
buttonadd.place(x=210,y=370,width=70,height=50)
#第六行
buttoninverse = tk.Button(root,font=("微软雅黑",20),text='±',bd='0.5',fg='black',command=lambda : pressfunction('±'))
buttoninverse.place(x=0,y=420,width=70,height=50)
button0 = tk.Button(root,font=("微软雅黑",20),text='0',bd='0.5',bg='white',command=lambda : pressnumber('0'))
button0.place(x=70,y=420,width=70,height=50)
buttonpoint = tk.Button(root,font=("微软雅黑",20),text='.',bd='0.5',fg='black',command=lambda : pressnumber('.'))
buttonpoint.place(x=140,y=420,width=70,height=50)
buttonvalue = tk.Button(root,font=("微软雅黑",20),text='=',bd='0.5',bg='orange',fg='black',command=lambda : pressequal())
buttonvalue.place(x=210,y=420,width=70,height=50)
#操作函数
lists = [] #设置一个变量 保存运算数字和符号的列表
ispressfunction = False #添加一个判断是否按下运算符号的标志,假设默认没有按下按钮
ispressnumber = False
ispressequal = False
#数字函数 判断是否按下数字 并获取数字将数字写在显示版上
def pressnumber(num):
global lists #全局化lists
global ispressfunction #按钮状态ispressfunction
global ispressequal #全局化等号状态ispressfunction
if ispressequal == False : #计算完一个结果后 重新输入数据
pass
else :
ispressequal = False #重新将等号设置为0
result1.set('')
if ispressfunction == False:
pass
else: #重新将运算符号状态设置为0
result1.set(0)
ispressfunction = False
oldnum = result1.get() #第一步判断界面的数字是否为0
if oldnum =='0': #如过界面上数字为0 则获取按下的数字
result1.set(num)
else: #如果界面上的而数字不是0 则链接上新按下的数字
newnum = oldnum + num
result1.set(newnum) #将按下的数字写到面板中
#按功能键函数 判断是否按下功能键
def pressfunction(stringlist):
global lists
global ispressfunction
global ispressequal
num = result1.get() #获取界面数字
lists.append(num) #将界面上的数字添加到lists列表中
shizi = '' #设置一个字符串将每次点击功能键后的式子显示到屏幕2上
ispressfunction = True
if stringlist in ['-','+','/','*','//']: #如果是常规运算符 加、减、乘、除、取余 那么直接将lists存入列表中
lists.append(stringlist) #将按下的运算符号保存到列表中
for each in lists :
shizi += each
if stringlist not in ['C','CE','←','±']: #如果按的不是这些特殊按键则将按键加入式子 之后显示在label2框中
result2.set(shizi)
if stringlist in ['√','x²','1/x']: #如果按的是开根号,平方根,倒数
if stringlist == '√': #如果取的数开根号
list1 = []
numnow = result1.get() #获取点击根号前 屏幕上的数字
list1.append("sqrt(" + numnow + ")") #将要在屏幕上输出的式子形式加入lists1中
sqrt1 = m.sqrt(int(numnow)) #用math模块计算结果
result2.set(list1) #将式子显示在label2中
result1.set(sqrt1) #将结果显示在lists2中
elif stringlist == 'x²': #获取点击平方前 屏幕上的数字 之后类似
list1 = []
numnow = result1.get()
list1.append("power(" + numnow + ",2)")
power1 = m.pow(int(numnow),2)
result2.set(list1)
result1.set(power1)
else:
list1 = []
numnow = result1.get()
list1.append("1/" + numnow)
countback1 = 1/int(numnow)
result2.set(list1)
result1.set(countback1)
if stringlist =='C': #如果按下的是'C'按键,则清空当前输入的数字,将其设为0
lists.pop() #以及之前存留在列表中的数
result1.set(0)
ispressfunction = False #添加一个判断是否按下运算符号的标志,假设默认没有按下按钮
ispressnumber = False
ispressequal = False
if stringlist =='CE': #如果按下的是'CE'按键,则清空列表内容,讲屏幕上的数字键设置为默认数字0
lists.clear()
result1.set(0)
result2.set('')
if stringlist =='←': #如果按下的是退格‘’,则选取当前数字第一位到倒数第二位
a = num[0:-1]
lists.pop() #将原来lists中添加的未退格的数字去除
result1.set(a)
if stringlist =='±': #如果按下的是取反
lists.pop() #将原来lists中添加的未取反的数字去除
num = str(int(0-int(num)))
result1.set(num)
#获取运算结果函数
def pressequal():
global lists
global ispressfunction
global ispressequal
ispressequal = True #将等号
curnum = result1.get() #设置当前数字变量,并获取添加到列表
lists.append(curnum) #将点下'='时的数字加入列表中
computrStr = ''.join(lists) #讲列表内容用join命令将字符串链接起来
endnum = eval(computrStr) #用eval命令运算字符串中的内容
result1.set(endnum) #讲运算结果显示到屏幕1
result2.set(computrStr) #将运算过程显示到屏幕2
lists.clear() #清空列表内容
root.mainloop()点赞1
评论