猫史档案馆


【Python作品分享】一个极其普通的计算器1.9【作品秀】

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


【作品展示】

center_image

 

【作品介绍】

又肝了一会肝出了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.cn


回复

上一页1 页 / 共 1下一页
沉著的飞叶龙uZWJ沉著的飞叶龙uZWJ

大家在评论区说出自己的想法吧,到底用eval,还是我的算法?

 

PS:新来的同学可以看之前的计算器版本(v1.1以上)都有详细的、带注释的计算器算法

点赞0


评论


沉著的飞叶龙uZWJ沉著的飞叶龙uZWJ

有人吗???

点赞0


评论


韶浅韶浅

作者可以将中缀转成前缀表达式建个手工栈求值a 就不用管括号了

点赞0


评论


沉著的飞叶龙uZWJ沉著的飞叶龙uZWJ

对于程序的优化,以后肯定会做的。

 

我现在是在问下一个版本的更新去向!

 

是否替换计算核心?

点赞0


评论


蒟蒻OIer1048576蒟蒻OIer1048576

2^喵00000000000000000000000吃烤cpu……

点赞0


评论


沉著的飞叶龙uZWJ沉著的飞叶龙uZWJ

各位注意一下,周日晚24点是发表看法的最后期限!过了这个时间点就没用了!作者就会按自己的思路更新了!

点赞0


评论


沉著的飞叶龙uZWJ沉著的飞叶龙uZWJ

怎么没有更多建议了??

点赞0


评论


LAG的1048576一枚LAG的1048576一枚

pi和e和sin/cos/tan可以:

s=content.replace('e','math.e').replace('π','math.pi')……#然后直接用eval

点赞1


评论