猫史档案馆


【计算器消息】

用户:沉著的飞叶龙uZWJ沉著的飞叶龙uZWJ查看:13 回复:9 评论:13 创建时间:2020-04-26T14:09:16


我发现以eval函数为核心的计算器很难加入三角函数运算。因为:

我没法钻进去修改eval函数的内部,所以我没法让eval函数计算含三角函数的代数式。那么我只能用”按下按钮把计算区的算式算出来然后带入三角函数求值“的方法。这个是可行的。

但是!

你想想会有多少个按钮……

sin弧度、cos弧度、tan弧度、sin角度、cos角度、tan角度、asin、acos、atan,一共9个按钮!全部加上会使计算器的排版混乱不堪,极大地损失计算器的喵。所以,我决定,

以eval函数为核心的tkinter计算器不会出现三角函数运算。

不过为了补偿你们,我又决定,未来再肝一个使用我自己的算法的计算器。

 

 

 


回复

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

1.0-1.7(easygui+trutle)版本使用的是我的算法,1.8-1.11(easygui+tkinter)使用的是eval算法。“tkinter+我的算法”版本会从2.0开始命名。记住哦!

点赞0


评论


少幽科技少幽科技

用exec("ans=sin(5)")之类的计算啊 

点赞0


评论


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

提点简单有用的点子吧,不然这个tkinter-eval计算器就没有1.12了,就永久不更新了

 

顺带加上拍打编程猫的图片……

 

emotion_编程猫_搓头emotion_编程猫_搓头emotion_编程猫_搓头emotion_编程猫_搓头emotion_编程猫_搓头emotion_编程猫_搓头emotion_编程猫_搓头emotion_编程猫_搓头

点赞0


评论


蒟蒻OIer1048576蒟蒻OIer1048576

试试正则计算器吧~喵~~~

点赞0


评论


蒟蒻OIer1048576蒟蒻OIer1048576

我也做计算器了

点赞0


评论


蒟蒻OIer1048576蒟蒻OIer1048576

我用eval函数加上了三角函数!

点赞0


评论


wrmdcxywrmdcxy

现在的计算机有INV按钮,就是逆运算哦~(sin=>asin之类的,可以用stringvar)

点赞0


评论


蒟蒻OIer1048576蒟蒻OIer1048576

from tkinter import *
from easygui import msgbox
import math
w = Tk()
equation = StringVar()
def num(n):
    equation.set(equation.get() + str(n))
sin,cos,tan = lambda n: math.cos(math.radians(n)),lambda n: math.tan(math.radians(n)),lambda n: math.sin(math.radians(n))
sinh,cosh,tanh = math.sin,math.cos,math.tan
asin,acos,atan = math.asin,math.acos,math.atan
deg,rad,sqrt = math.degrees,math.radians,math.sqrt
binary,october,hexadecimal = lambda n: bin(n)[2:],lambda n: oct(n)[2:],lambda n: hex(n)[2:]
def calculate():
    try:
        msgbox(eval(equation.get().replace('×','*').replace('÷','/').replace('^','**').replace('fact','math.factorial')))
    except Exception as e:
        msgbox('出现错误:'+str(e))
    equation.set('')
def function(name):
    equation.set(name + '(' + equation.get() + ')')
w.geometry('1000x1000')
Button(w, text=0, command=lambda: num(0), width=7, height=2, font=('Arial',20)).grid(row=1,column=0)
Button(w, text=1, command=lambda: num(1), width=7, height=2, font=('Arial',20)).grid(row=1,column=1)
Button(w, text=2, command=lambda: num(2), width=7, height=2, font=('Arial',20)).grid(row=1,column=2)
Button(w, text=3, command=lambda: num(3), width=7, height=2, font=('Arial',20)).grid(row=2,column=0)
Button(w, text=4, command=lambda: num(4), width=7, height=2, font=('Arial',20)).grid(row=2,column=1)
Button(w, text=5, command=lambda: num(5), width=7, height=2, font=('Arial',20)).grid(row=2,column=2)
Button(w, text=6, command=lambda: num(6), width=7, height=2, font=('Arial',20)).grid(row=3,column=0)
Button(w, text=7, command=lambda: num(7), width=7, height=2, font=('Arial',20)).grid(row=3,column=1)
Button(w, text=8, command=lambda: num(8), width=7, height=2, font=('Arial',20)).grid(row=3,column=2)
Button(w, text=9, command=lambda: num(9), width=7, height=2, font=('Arial',20)).grid(row=4,column=1)
Button(w, text='.', command=lambda: num('.'), width=7, height=2, font=('Arial',20)).grid(row=5,column=3)
Button(w, text='+', command=lambda: num('+'), width=7, height=2, font=('Arial',20)).grid(row=1, column=3)
Button(w, text='-', command=lambda: num('-'), width=7, height=2, font=('Arial',20)).grid(row=2, column=3)
Button(w, text='×', command=lambda: num('×'), width=7, height=2, font=('Arial',20)).grid(row=3, column=3)
Button(w, text='÷', command=lambda: num('÷'), width=7, height=2, font=('Arial',20)).grid(row=4, column=3)
Button(w, text='^', command=lambda: num('^'), width=7, height=2, font=('Arial',20)).grid(row=4, column=2)
Button(w, text='(', command=lambda: num('('), width=7, height=2, font=('Arial',20)).grid(row=5, column=1)
Button(w, text=')', command=lambda: num(')'), width=7, height=2, font=('Arial',20)).grid(row=5, column=2)
Button(w, text='阶乘', command=lambda: function('fact'), width=7, height=2, font=('Arial',20)).grid(row=5, column=0)
Button(w, text='sin(角度)', command=lambda: function('sin'), width=7, height=2, font=('Arial',20)).grid(row=6, column=0)
Button(w, text='cos(角度)', command=lambda: function('cos'), width=7, height=2, font=('Arial',20)).grid(row=6, column=1)
Button(w, text='tan(角度)', command=lambda: function('tan'), width=7, height=2, font=('Arial',20)).grid(row=6, column=2)
Button(w, text='sin(弧度)', command=lambda: function('sinh'), width=7, height=2, font=('Arial',20)).grid(row=7, column=0)
Button(w, text='cos(弧度)', command=lambda: function('cosh'), width=7, height=2, font=('Arial',20)).grid(row=7, column=1)
Button(w, text='tan(弧度)', command=lambda: function('tanh'), width=7, height=2, font=('Arial',20)).grid(row=7, column=2)
Button(w, text='asin', command=lambda: function('asin'), width=7, height=2, font=('Arial',20)).grid(row=8, column=0)
Button(w, text='acos', command=lambda: function('acos'), width=7, height=2, font=('Arial',20)).grid(row=8, column=1)
Button(w, text='atan', command=lambda: function('atan'), width=7, height=2, font=('Arial',20)).grid(row=8, column=2)
Button(w, text='平方根', command=lambda: function('sqrt'), width=7, height=2, font=('Arial',20)).grid(row=10, column=0)
Button(w, text='弧度-角度', command=lambda: function('deg'), width=7, height=2, font=('Arial',20)).grid(row=9, column=0)
Button(w, text='角度-弧度', command=lambda: function('rad'), width=7, height=2, font=('Arial',20)).grid(row=9, column=1)
Button(w, text='绝对值', command=lambda: function('abs'), width=7, height=2, font=('Arial',20)).grid(row=9, column=2)
Button(w, text='二进制', command=lambda: function('binary'), width=7, height=2, font=('Arial',20)).grid(row=10, column=1)
Button(w, text='八进制', command=lambda: function('october'), width=7, height=2, font=('Arial',20)).grid(row=10, column=2)
Button(w, text='十六进制', command=lambda: function('hexadecimal'), width=7, height=2, font=('Arial',20)).grid(row=10, column=3)

Button(w, text='=', command=calculate, width=7, height=2, font=('Arial',20)).grid(row=4,column=0)
Label(w, textvar=equation, font=('Arial',30)).grid(row=0,column=1)
w.title('计算器')
w.mainloop()

点赞0


评论


煤黑烧饼煤黑烧饼

参考一下win10的排版

center_image

点赞0


评论