Python tkinter计算器功能
Jhze 人气:0python版本:3.5
一.计算器的功能描述
今天我们用python来实现一个计算器。首先,计算器分为两部分:一部分是初级页面,包括简单的加减乘除四则运算。第二部分:是高级页面,包括常见的三角函数、对数、括号、等参数运算。其次,在初级页面,能进行简单的初级运算,并在初级页面设置高级按钮,并让其高亮显示,用户点击高级按钮后,会切换到高级页面。来到高级页面,让扩展的功能高亮显示,同时可以参加高级运算。并且在高级页面留有初级页面按钮,点击可以回到初级页面。相关效果截图在文章末尾。
整个运算保留小数点10位有效数字。
二.代码实现
Calculate.py的实现
我们此次属于python自带的一个库tkinter,有自带的GUI用起来很方便。
1. 导包
import tkinter as tk import tkinter.messagebox import re import math from functions import *
import导入包,as是给tkinter重新起了个名字,messagebox是一个错误提示框(如:分母为0,会错误提示)
functions.py是一个自定义函数,计算数值时用。
2. 初始化初级界面
root = tk.Tk() root.minsize(300, 400) # 窗口大小300*400 root.resizable(0, 0) root.title('Jhze的计算器') # 计算器名字
3. 创建初级页面
生成初级页面的所有按钮,这里看起来代码很累赘,尝试用循环实现,但是循环生成的按钮样子不好看,没有调到想要的效果。所以就选择这样一个一个的生成。
这里利用button中一个很重要的参数command来回调函数,具体就是你可以通过点击按钮来获取想应的值,并且做相关的操作。
# 运算符号按钮 # 第一行 btnac = tkinter.Button(root, text='AC', bd=0.5, font=('黑体', 20), fg='orange', command=lambda \ x='AC': buttonClick(x)) btnac.place(x=0, y=150, width=75, height=50) btnback = tkinter.Button(root, text='←', font=('微软雅黑', 20), fg='#4F4F4F', bd=0.5, command=lambda \ x='←': buttonClick(x)) btnback.place(x=75, y=150, width=75, height=50) btndivi = tkinter.Button(root, text='^', font=('微软雅黑', 20), fg='#4F4F4F', bd=0.5, command=lambda \ x='^': buttonClick(x)) btndivi.place(x=150, y=150, width=75, height=50) btnmul = tkinter.Button(root, text='+', font=('微软雅黑', 20), fg="#4F4F4F", bd=0.5, command=lambda \ x='+': buttonClick(x)) btnmul.place(x=225, y=150, width=75, height=50) # 第二行 btn7 = tkinter.Button(root, text='7', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='7': buttonClick(x)) btn7.place(x=0, y=200, width=75, height=50) btn8 = tkinter.Button(root, text='8', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='8': buttonClick(x)) btn8.place(x=75, y=200, width=75, height=50) btn9 = tkinter.Button(root, text='9', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='9': buttonClick(x)) btn9.place(x=150, y=200, width=75, height=50) btnsub = tkinter.Button(root, text='-', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='-': buttonClick(x)) btnsub.place(x=225, y=200, width=75, height=50) # 第三行 btn4 = tkinter.Button(root, text='4', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='4': buttonClick(x)) btn4.place(x=0, y=250, width=75, height=50) btn5 = tkinter.Button(root, text='5', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='5': buttonClick(x)) btn5.place(x=75, y=250, width=75, height=50) btn6 = tkinter.Button(root, text='6', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='6': buttonClick(x)) btn6.place(x=150, y=250, width=75, height=50) btnadd = tkinter.Button(root, text='×', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='×': buttonClick(x)) btnadd.place(x=225, y=250, width=75, height=50) # 第四行 btn1 = tkinter.Button(root, text='1', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='1': buttonClick(x)) btn1.place(x=0, y=300, width=75, height=50) btn2 = tkinter.Button(root, text='2', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='2': buttonClick(x)) btn2.place(x=75, y=300, width=75, height=50) btn3 = tkinter.Button(root, text='3', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='3': buttonClick(x)) btn3.place(x=150, y=300, width=75, height=50) btnechu = tkinter.Button(root, text='÷', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='÷': buttonClick(x)) btnechu.place(x=225, y=300, width=75, height=50) # 第五行 btnper = tkinter.Button(root, text='高级', font=('微软雅黑', 20), fg='orange', bd=0.5, command=lambda x='高级': buttonClick(x)) btnper.place(x=0, y=350, width=75, height=50) btn0 = tkinter.Button(root, text='0', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='0': buttonClick(x)) btn0.place(x=75, y=350, width=75, height=50) btnpoint = tkinter.Button(root, text='.', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='.': buttonClick(x)) btnpoint.place(x=150, y=350, width=75, height=50) btnequ = tkinter.Button(root, text='=', bg='orange', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda x='=': buttonClick(x)) btnequ.place(x=225, y=350, width=75, height=50)
4.绘制输出文本框
contentVar = tkinter.StringVar(root, '') contentEntry = tkinter.Entry(root, textvariable=contentVar, state='readonly', font=("Arial", 12)) contentEntry.place(x=0, y=110, width=300, height=40)
5.低级页面的响应事件处理
这里我们通过buttonClick()来响应事件。
1.如果btn是‘0123456789’数值,直接赋值给content。
2.如果btn是‘AC’,响应清屏
3.如果btn是‘←’,回退一个字符,其实这里最方便的是用切片法很容易实现
4.如果btn是‘=’,则将content这个字符串计算值,并输出。因为是初级页面,只有简单的四则运算,完全可以用python自带的函数eval()来计算值。
def buttonClick(btn): content = contentVar.get() if content.startswith('.'): # 小数点前加0 content = '0' + content if btn in '0123456789': content += btn elif btn == '.': lastPart = re.split(r'\+|-|\*|/', content)[-1] if '.' in lastPart: tk.messagebox.showerror('错误', 'Input Error') return else: content += btn elif btn == 'AC': content = '' elif btn == '=': try: for operat in content: if operat == '÷': content = content.replace('÷', '/') elif operat == '×': content = content.replace('×', '*') value = eval(content) content = str(round(value, 10)) except: tk.messagebox.showerror('错误', 'VALUE ERROR') return elif btn in operators: if content.endswith(operators): tk.messagebox.showerror('错误', 'FORMAT ERROR') return content += btn elif btn == '^': n = content.split('.') if all(map(lambda x: x.isdigit(), n)): content = eval(content)*eval(content) else: tk.messagebox.showerror('错误', 'Input Error') return elif btn == '←': # 如果按下的是退格‘',则选取当前数字第一位到倒数第二位 content = content[0:-1] elif btn == '高级':
其实此时已经实现了一个简单的初级页面,能进行四则运算,并且能输出相应的值。就差高级这个接口的实现啦,接下来就让我们来实现高级这个按钮的功能吧。
6.低级页面中高级按钮的实现
[注意]:以下代码全部都是在“高级”这个按钮下来实现的,注意python语言的缩进。也就是在上述的 elif btn==‘高级’:之下实现。
点击高级按钮之后,响应对应事件,因为我们页面大小不变,所以我们需要调用btnac.destroy() 来销毁低级页面的按钮,同理调用:btn1.destroy()、btn2.destroy()等等。也就意味着把初级页面的按钮全部销毁,重新布局。此时有7行按钮生成。
这里我说一下最初的构思,本来想点击高级按钮之后,将原来的按钮缩小比例,并且增加新的高级按钮。这样可以实现,而且减少页面布局代码,但是这样做的同时,初级界面的那个‘高级’按钮还会在,曾试图改变它的名称,但效果不是很好。并且高级与初级页面在计算的时候都是用的同一种方法,通过command回调函数来实现。高级页面需要正则表达式对点击获取的字符串进行处理。所以出现诸多问题。因此没有用页面按钮比例缩小的那构思。而是直接将初级页面的按钮全部清理,重新布局。这样就解决了那些问题。也完成了高级<---->低级页面的来回跳转。看起来有种缩放的效果。请读者自行体会。
contentEntry.place(x=0, y=45, width=300, height=40) # 重新绘制输出框 # 运算符号按钮 # 第一行 btncsc = tkinter.Button(root, text='csc', font=('微软雅黑', 20), bg=('#96CDCD'), fg=('#4F4F4F'), bd=0.5, command=lambda x='csc': buttonClick1(x)) btncsc.place(x=0, y=85, width=60, height=45) btnrad = tkinter.Button(root, text='rad', font=('微软雅黑', 20), bg=('#96CDCD'), fg=('#4F4F4F'), bd=0.5, command=lambda x='rad': buttonClick1(x)) btnrad.place(x=60, y=85, width=60, height=45) btnsin = tkinter.Button(root, text='sin', font=('微软雅黑', 20), bg=('#96CDCD'), fg=('#4F4F4F'), bd=0.5, command=lambda x='sin': buttonClick1(x)) btnsin.place(x=120, y=85, width=60, height=45) btncos = tkinter.Button(root, text='cos', font=('微软雅黑', 20), bg=('#96CDCD'), fg=('#4F4F4F'), bd=0.5, command=lambda x='cos': buttonClick1(x)) btncos.place(x=180, y=85, width=60, height=45) btntan = tkinter.Button(root, text='tan', font=('微软雅黑', 20), bg=('#96CDCD'), fg=('#4F4F4F'), bd=0.5, command=lambda x='tan': buttonClick1(x)) btntan.place(x=240, y=85, width=60, height=45) # 第二行 btnxsec = tkinter.Button(root, text='sec', font=('微软雅黑', 20), bg=('#96CDCD'), fg=('#4F4F4F'), bd=0.5, command=lambda x='sec': buttonClick1(x)) btnxsec.place(x=0, y=130, width=60, height=45) btnlog = tkinter.Button(root, text='lg', font=('微软雅黑', 20), bg=('#96CDCD'), fg=('#4F4F4F'), bd=0.5, command=lambda x='lg': buttonClick1(x)) btnlog.place(x=60, y=130, width=60, height=45) btnln = tkinter.Button(root, text='ln', font=('微软雅黑', 20), bg=('#96CDCD'), fg=('#4F4F4F'), bd=0.5, command=lambda x='ln': buttonClick1(x)) btnln.place(x=120, y=130, width=60, height=45) btnleft = tkinter.Button(root, text='(', font=('微软雅黑', 20), bg=('#96CDCD'), fg=('#4F4F4F'), bd=0.5, command=lambda x='(': buttonClick1(x)) btnleft.place(x=180, y=130, width=60, height=45) btnrigh = tkinter.Button(root, text=')', font=('微软雅黑', 20), bg=('#96CDCD'), fg=('#4F4F4F'), bd=0.5, command=lambda x=')': buttonClick1(x)) btnrigh.place(x=240, y=130, width=60, height=45) # 第三行 btnaxy = tkinter.Button(root, text='x^y', bd=0.5, font=('黑体', 20), bg=('#96CDCD'), command=lambda \ x='x^y': buttonClick1(x)) btnaxy.place(x=0, y=175, width=60, height=45) btnac.destroy() btnac1 = tkinter.Button(root, text='AC', bd=0.5, font=('黑体', 20), fg='orange', command=lambda \ x='AC': buttonClick1(x)) btnac1.place(x=60, y=175, width=60, height=45) btnback.destroy() btnback1 = tkinter.Button(root, text='←', font=('微软雅黑', 20), fg='#4F4F4F', bd=0.5, command=lambda \ x='←': buttonClick1(x)) btnback1.place(x=120, y=175, width=60, height=45) btndivi.destroy() btndivi1 = tkinter.Button(root, text='^', font=('微软雅黑', 20), fg='#4F4F4F', bd=0.5, command=lambda \ x='^': buttonClick1(x)) btndivi1.place(x=180, y=175, width=60, height=45) btnmul.destroy() btnmul1 = tkinter.Button(root, text='+', font=('微软雅黑', 20), fg="#4F4F4F", bd=0.5, command=lambda \ x='+': buttonClick1(x)) btnmul1.place(x=240, y=175, width=60, height=45) # 第四行 btnx = tkinter.Button(root, text='X!', font=('微软雅黑', 20), bg=('#96CDCD'), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='X!': buttonClick1(x)) btnx.place(x=0, y=220, width=60, height=45) btn7.destroy() btn71 = tkinter.Button(root, text='7', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='7': buttonClick1(x)) btn71.place(x=60, y=220, width=60, height=45) btn8.destroy() btn81 = tkinter.Button(root, text='8', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='8': buttonClick1(x)) btn81.place(x=120, y=220, width=60, height=45) btn9.destroy() btn91 = tkinter.Button(root, text='9', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='9': buttonClick1(x)) btn91.place(x=180, y=220, width=60, height=45) btnsub.destroy() btnsub1 = tkinter.Button(root, text='-', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='-': buttonClick1(x)) btnsub1.place(x=240, y=220, width=60, height=45) # 第五行 btn4x = tkinter.Button(root, text='1/X', font=('微软雅黑', 20), bg=('#96CDCD'), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='1/X': buttonClick1(x)) btn4x.place(x=0, y=265, width=60, height=45) btn4.destroy() btn41 = tkinter.Button(root, text='4', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='4': buttonClick1(x)) btn41.place(x=60, y=265, width=60, height=45) btn5.destroy() btn51 = tkinter.Button(root, text='5', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='5': buttonClick1(x)) btn51.place(x=120, y=265, width=60, height=45) btn6.destroy() btn61 = tkinter.Button(root, text='6', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='6': buttonClick1(x)) btn61.place(x=180, y=265, width=60, height=45) btnadd.destroy() btnadd1 = tkinter.Button(root, text='×', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='×': buttonClick1(x)) btnadd1.place(x=240, y=265, width=60, height=45) # 第六行 btnpi = tkinter.Button(root, text='π', font=('微软雅黑', 20), bg=('#96CDCD'), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='π': buttonClick1(x)) btnpi.place(x=0, y=310, width=60, height=45) btnpi.flash() btn1.destroy() btn11 = tkinter.Button(root, text='1', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='1': buttonClick1(x)) btn11.place(x=60, y=310, width=60, height=45) btn2.destroy() btn21 = tkinter.Button(root, text='2', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='2': buttonClick1(x)) btn21.place(x=120, y=310, width=60, height=45) btn3.destroy() btn31 = tkinter.Button(root, text='3', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='3': buttonClick1(x)) btn31.place(x=180, y=310, width=60, height=45) btnechu.destroy() btnechu1 = tkinter.Button(root, text='÷', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='÷': buttonClick1(x)) btnechu1.place(x=240, y=310, width=60, height=45) # 第七行 btnperr = tkinter.Button(root, text='低级', font=('微软雅黑', 20), fg='orange', bd=0.5, command=lambda x='低级': buttonClick1(x)) btnperr.place(x=0, y=355, width=60, height=45) btnper.destroy() btnper1 = tkinter.Button(root, text='e', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda x='e': buttonClick1(x)) btnper1.place(x=60, y=355, width=60, height=45) btn0.destroy() btn01 = tkinter.Button(root, text='0', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='0': buttonClick1(x)) btn01.place(x=120, y=355, width=60, height=45) btnpoint.destroy() btnpoint1 = tkinter.Button(root, text='.', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='.': buttonClick1(x)) btnpoint1.place(x=180, y=355, width=60, height=45) btnequ.destroy() btnequ1 = tkinter.Button(root, text='=', bg='orange', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda x='=': buttonClick1(x)) btnequ1.place(x=240, y=355, width=60, height=45)
7.高级页面的响应事件的处理
先来说明一下计算器计算某个表达式用到的原理:
比如要计算这个表达式:2+3*(sin(0)+lg(10))-2
1.首先用正则表达式对这个字符串处理,计算出sin(0)=0,字符串变为:2+3*(0+lg(10))-2
2.再用正则表达式处理字符串,计算出lg(10)=1,字符串变为:2+3*(0+1)-2
3.再用正则表达式处理字符串,计算括号(0+1) =1,此时字符串变成:3+3*1-2
4.四则运算直接调用python自带的函数eval()直接计算结果。
笔者用此思想来实现下述高级页面表达式的计算。
【注】:重要实现的要点说明
1.高级页面依然保留初级页面所有布局、功能,在此基础上增加了很多新功能。
2.在高级页面输完表达式,当我们点击‘=’时,计算某个表达式值。用到上述思想。
3.我们以计算一个表达式中含有sin(x)为例来进行简单的说明:
(1)我们首先判断sin是不是在某个表达式中。
(2)使用正则表达式:strsin = r’sin(\d+)|sin(-?\d+.\d+)‘来提取如相应的表达式。
(3) 此时得到是一个字符串表达式’sin(2)’,我们再用正则表达式提取里面的数字value。调用math库里面是sin函数。来计算sin(2), 如:value = str(sin_t(float(value))) 这里的sin_t()是一个自定义函数。在下述functions.py文件里面。
(4)此时我们将计算得到的数值,调用replace() 进行替换。就能达到想要的效果了哦。
别的表达式都是用的同样的方法,这里不再赘述。请读者根据源代码自行体会。
def buttonClick1(btn): content = contentVar.get() if content.startswith('.'): # 小数点前加0 content = '0' + content if btn in '0123456789()': content += btn elif btn == '.': lastPart = re.split(r'\+|-|\*|/', content)[-1] if '.' in lastPart: tk.messagebox.showerror('错误', 'Input Error') return else: content += btn elif btn == '^': n = content.split('.') if all(map(lambda x: x.isdigit(), n)): content = eval(content) * eval(content) else: tk.messagebox.showerror('错误', 'Input Error') return elif btn == 'AC': content = '' elif btn == '=': try: for operat in content: if operat == '÷': content = content.replace('÷', '/') elif operat == '×': content = content.replace('×', '*') elif operat == '^': content = content.replace('^', '**') strsin = r'sin\(\d+\)|sin\(\-?\d+\.\d+\)' if 'sin' in content: m = re.search(strsin, content) if m is not None: exchange = m.group() exchange1 = exchange if '.' in exchange: exchange = re.search("\-?\d+\.\d+", exchange) value = exchange.group() value = str(sin_t(float(value))) content = content.replace(exchange1, value) else: exchange = re.search("\-?\d+", exchange) value = exchange.group() value = str(sin_t(float(value))) content = content.replace(exchange1, value) strcos = r'cos\(\d+\)|cos\(\-?\d+\.\d+\)' if 'cos' in content: m = re.search(strcos, content) if m is not None: exchange = m.group() exchange1 = exchange if '.' in exchange: exchange = re.search("\-?\d+\.\d+", exchange) value = exchange.group() value = str(cos_t(float(value))) content = content.replace(exchange1, value) else: exchange = re.search("\-?\d+", exchange) value = exchange.group() value = str(cos_t(float(value))) content = content.replace(exchange1, value) strtan = r'tan\(\d+\)|tan\(\-?\d+\.\d+\)' if 'tan' in content: m = re.search(strtan, content) if m is not None: exchange = m.group() exchange1 = exchange if '.' in exchange: exchange = re.search("\-?\d+\.\d+", exchange) value = exchange.group() value = str(tan_t(float(value))) content = content.replace(exchange1, value) else: exchange = re.search("\-?\d+", exchange) value = exchange.group() value = str(tan_t(float(value))) content = content.replace(exchange1, value) strsec = r'sec\(\-?\d+\)|sec\(\-?\d+\.\d+\)' if 'sec' in content: m = re.search(strsec, content) if m is not None: exchange = m.group() exchange1 = exchange if '.' in exchange: exchange = re.search("\-?\d+\.\d+", exchange) value = exchange.group() value = str(sec_t(float(value))) content = content.replace(exchange1, value) else: exchange = re.search("\-?\d+", exchange) value = exchange.group() value = str(sec_t(float(value))) content = content.replace(exchange1, value) strcsc = r'csc\(\d+\)' if 'csc' in content: m = re.search(strcsc, content) if m is not None: exchange = m.group() exchange1 = exchange if '.' in exchange: exchange = re.search("\-?\d+\.\d+", exchange) value = exchange.group() value = str(csc_t(float(value))) content = content.replace(exchange1, value) else: exchange = re.search("\-?\d+", exchange) value = exchange.group() value = str(csc_t(float(value))) content = content.replace(exchange1, value) strlg = r'lg\(\-?\d+\)|lg\(\-?\d+\.\d+\)' if 'lg' in content: m = re.search(strlg, content) if m is not None: exchange = m.group() exchange1 = exchange if '.' in exchange: exchange = re.search("\-?\d+\.\d+", exchange) value = exchange.group() if float(value) <= 0: tk.messagebox.showerror('错误', 'FORMAT ERROR') else: value = str(lg_t(float(value))) content = content.replace(exchange1, value) else: exchange = re.search("\-?\d+", exchange) value = exchange.group() if int(value)<=0 : tk.messagebox.showerror('错误', 'FORMAT ERROR') else: value = str(lg_t(float(value))) content = content.replace(exchange1, value) strln = r'ln\(\-?\d+\)|ln\(\-?\d+\.\d+\)' if 'ln' in content: m = re.search(strln, content) if m is not None: exchange = m.group() exchange1 = exchange if '.' in exchange: exchange = re.search("\-?\d+\.\d+", exchange) value = exchange.group() if float(value) <= 0: tk.messagebox.showerror('错误', 'FORMAT ERROR') else: value = str(ln_t(float(value))) content = content.replace(exchange1, value) else: exchange = re.search("\-?\d+", exchange) value = exchange.group() if int(value) <= 0: tk.messagebox.showerror('错误', 'FORMAT ERROR') else: value = str(ln_t(float(value))) content = content.replace(exchange1, value) value = eval(content) content = str(round(value, 10)) except ZeroDivisionError: tk.messagebox.showerror('错误', 'VALUE ERROR') return elif btn in operators: if content.endswith(operators): tk.messagebox.showerror('错误', 'FORMAT ERROR') return content += btn elif btn == 'e': content = 2.7182818284 elif btn == 'π': content = 3.1415926535 elif btn == '1/X': content = reciprocal(float(content)) elif btn == 'X!': content = factorial(int(content)) elif btn == 'x^y': content += '^' elif btn == 'sin': content += 'sin(' elif btn == 'cos': content += 'cos(' elif btn == 'tan': content += 'tan(' elif btn == 'sec': content += 'sec(' elif btn == 'csc': content += 'csc(' elif btn == 'lg': content += 'lg(' elif btn == 'ln': content += 'ln(' elif btn == '←': # 如果按下的是退格‘',则选取当前数字第一位到倒数第二位 content = content[0:-1]
8.高级页面中低级按钮实现
这里还是用到的同样思想。对所有的高级按钮全部销毁。重新布局新的按钮。这里不再赘述。
elif btn == '低级': contentEntry.place(x=0, y=110, width=300, height=40) #第一行 btncsc.destroy() btnrad.destroy() btnsin.destroy() btncos.destroy() btntan.destroy() # 第二行 btnxsec.destroy() btnlog.destroy() btnln.destroy() btnleft.destroy() btnrigh.destroy() # 第三行 btnaxy.destroy() btnac1.destroy() btnback1.destroy() btndivi1.destroy() btnmul1.destroy() # 第四行 btnx.destroy() btn71.destroy() btn81.destroy() btn91.destroy() btnsub1.destroy() # 第五行 btn4x.destroy() btn41.destroy() btn51.destroy() btn61.destroy() btnadd1.destroy() # 第六行 btnpi.destroy() btn11.destroy() btn21.destroy() btn31.destroy() btnechu1.destroy() # 第七行 btnperr.destroy() btnper1.destroy() btn01.destroy() btnpoint1.destroy() btnequ1.destroy() # 第一行 btnac = tkinter.Button(root, text='AC', bd=0.5, font=('黑体', 20), fg='orange', command=lambda \ x='AC': buttonClick(x)) btnac.flash() btnac.place(x=0, y=150, width=75, height=50) btnback = tkinter.Button(root, text='←', font=('微软雅黑', 20), fg='#4F4F4F', bd=0.5, command=lambda \ x='←': buttonClick(x)) btnback.place(x=75, y=150, width=75, height=50) btndivi = tkinter.Button(root, text='^', font=('微软雅黑', 20), fg='#4F4F4F', bd=0.5, command=lambda \ x='^': buttonClick(x)) btndivi.place(x=150, y=150, width=75, height=50) btnmul = tkinter.Button(root, text='+', font=('微软雅黑', 20), fg="#4F4F4F", bd=0.5, command=lambda \ x='+': buttonClick(x)) btnmul.place(x=225, y=150, width=75, height=50) # 第二行 btn7 = tkinter.Button(root, text='7', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='7': buttonClick(x)) btn7.place(x=0, y=200, width=75, height=50) btn8 = tkinter.Button(root, text='8', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='8': buttonClick(x)) btn8.place(x=75, y=200, width=75, height=50) btn9 = tkinter.Button(root, text='9', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='9': buttonClick(x)) btn9.place(x=150, y=200, width=75, height=50) btnsub = tkinter.Button(root, text='-', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='-': buttonClick(x)) btnsub.place(x=225, y=200, width=75, height=50) # 第三行 btn4 = tkinter.Button(root, text='4', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='4': buttonClick(x)) btn4.place(x=0, y=250, width=75, height=50) btn5 = tkinter.Button(root, text='5', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='5': buttonClick(x)) btn5.place(x=75, y=250, width=75, height=50) btn6 = tkinter.Button(root, text='6', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='6': buttonClick(x)) btn6.place(x=150, y=250, width=75, height=50) btnadd = tkinter.Button(root, text='×', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='×': buttonClick(x)) btnadd.place(x=225, y=250, width=75, height=50) # 第四行 btn1 = tkinter.Button(root, text='1', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='1': buttonClick(x)) btn1.place(x=0, y=300, width=75, height=50) btn2 = tkinter.Button(root, text='2', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='2': buttonClick(x)) btn2.place(x=75, y=300, width=75, height=50) btn3 = tkinter.Button(root, text='3', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='3': buttonClick(x)) btn3.place(x=150, y=300, width=75, height=50) btnechu = tkinter.Button(root, text='÷', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='÷': buttonClick(x)) btnechu.place(x=225, y=300, width=75, height=50) # 第五行 btnper = tkinter.Button(root, text='高级', font=('微软雅黑', 20), fg='orange', bd=0.5, command=lambda x='高级': buttonClick(x)) btnper.place(x=0, y=350, width=75, height=50) btn0 = tkinter.Button(root, text='0', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='0': buttonClick(x)) btn0.place(x=75, y=350, width=75, height=50) btnpoint = tkinter.Button(root, text='.', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda \ x='.': buttonClick(x)) btnpoint.place(x=150, y=350, width=75, height=50) btnequ = tkinter.Button(root, text='=', bg='orange', font=('微软雅黑', 20), fg=('#4F4F4F'), bd=0.5, command=lambda x='=': buttonClick(x)) btnequ.place(x=225, y=350, width=75, height=50) contentVar.set(content) contentVar.set(content)
9.运行代码
这里所有的代码以及完成。最后还差个functions.py源码啦。
operators = ('÷', '×', '-', '+', '=', '.') root.mainloop()
10.functions.py实现
本来笔者想要用泰勒公式来计算复杂的表达式。但是没有得到想要的结果。直接调用math库啦,感兴趣的读者可以尝试用泰勒公式计算
#!/usr/bin/env python __author__: "Jhze dnowz" import math # 计算机倒数 def reciprocal(value): value = round(float(1) / (float(value)), 10) return value # 计算阶乘 def factorial(value): sum = 1 if value==0 or value==1: sum =1 for i in range(value): sum += sum * i return sum # 计算sin # def sin(x): # e = 10^(-15) # sum = 0 # evl = x # n = 1 # while(abs(evl)>e): # sum = sum+evl # a = (-1)**n # b = evl**(2*n+1) # c = factorial((2*n+1)) # evl = a*b/c # n +=1 # # print(sin(1)) # 计算sin def sin_t(x): return round(math.sin(x),10) # 计算cos def cos_t(x): return round(math.cos(x), 10) # 计算tan def tan_t(x): return round(math.tan(x), 10) # 计算csc def csc_t(x): return round(float(1)/math.sin(x), 10) # 计算sec def sec_t(x): return round(float(1)/math.cos(x), 10) # 计算lg def lg_t(x): return round(math.log10(x), 10) # 计算ln def ln_t(x): return round(math.log(x, math.e), 10)
11.相关效果图
12.动态效果图
加载全部内容