Python?tkinter實(shí)現(xiàn)計(jì)算器功能
本文實(shí)例為大家分享了Python tkinter實(shí)現(xiàn)計(jì)算器功能的具體代碼,供大家參考,具體內(nèi)容如下
python版本:3.5
一.計(jì)算器的功能描述
今天我們用python來(lái)實(shí)現(xiàn)一個(gè)計(jì)算器。首先,計(jì)算器分為兩部分:一部分是初級(jí)頁(yè)面,包括簡(jiǎn)單的加減乘除四則運(yùn)算。第二部分:是高級(jí)頁(yè)面,包括常見(jiàn)的三角函數(shù)、對(duì)數(shù)、括號(hào)、等參數(shù)運(yùn)算。其次,在初級(jí)頁(yè)面,能進(jìn)行簡(jiǎn)單的初級(jí)運(yùn)算,并在初級(jí)頁(yè)面設(shè)置高級(jí)按鈕,并讓其高亮顯示,用戶點(diǎn)擊高級(jí)按鈕后,會(huì)切換到高級(jí)頁(yè)面。來(lái)到高級(jí)頁(yè)面,讓擴(kuò)展的功能高亮顯示,同時(shí)可以參加高級(jí)運(yùn)算。并且在高級(jí)頁(yè)面留有初級(jí)頁(yè)面按鈕,點(diǎn)擊可以回到初級(jí)頁(yè)面。相關(guān)效果截圖在文章末尾。
整個(gè)運(yùn)算保留小數(shù)點(diǎn)10位有效數(shù)字。
二.代碼實(shí)現(xiàn)
Calculate.py的實(shí)現(xiàn)
我們此次屬于python自帶的一個(gè)庫(kù)tkinter,有自帶的GUI用起來(lái)很方便。
1. 導(dǎo)包
import tkinter as tk import tkinter.messagebox import re import math from functions import *
import導(dǎo)入包,as是給tkinter重新起了個(gè)名字,messagebox是一個(gè)錯(cuò)誤提示框(如:分母為0,會(huì)錯(cuò)誤提示)
functions.py是一個(gè)自定義函數(shù),計(jì)算數(shù)值時(shí)用。
2. 初始化初級(jí)界面
root = tk.Tk() root.minsize(300, 400) ? ? ?# 窗口大小300*400 root.resizable(0, 0) root.title('Jhze的計(jì)算器') ? ?# 計(jì)算器名字
3. 創(chuàng)建初級(jí)頁(yè)面
生成初級(jí)頁(yè)面的所有按鈕,這里看起來(lái)代碼很累贅,嘗試用循環(huán)實(shí)現(xiàn),但是循環(huán)生成的按鈕樣子不好看,沒(méi)有調(diào)到想要的效果。所以就選擇這樣一個(gè)一個(gè)的生成。
這里利用button中一個(gè)很重要的參數(shù)command來(lái)回調(diào)函數(shù),具體就是你可以通過(guò)點(diǎn)擊按鈕來(lái)獲取想應(yīng)的值,并且做相關(guān)的操作。
# 運(yùn)算符號(hào)按鈕 # 第一行 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='高級(jí)', font=('微軟雅黑', 20), fg='orange', bd=0.5, ? ? ? ? ? ? ? ? ? ? ? ? command=lambda x='高級(jí)': 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.低級(jí)頁(yè)面的響應(yīng)事件處理
這里我們通過(guò)buttonClick()來(lái)響應(yīng)事件。
1.如果btn是‘0123456789’數(shù)值,直接賦值給content。
2.如果btn是‘AC’,響應(yīng)清屏
3.如果btn是‘←’,回退一個(gè)字符,其實(shí)這里最方便的是用切片法很容易實(shí)現(xiàn)
4.如果btn是‘=’,則將content這個(gè)字符串計(jì)算值,并輸出。因?yàn)槭浅跫?jí)頁(yè)面,只有簡(jiǎn)單的四則運(yùn)算,完全可以用python自帶的函數(shù)eval()來(lái)計(jì)算值。
def buttonClick(btn): ? ? content = contentVar.get() ? ? if content.startswith('.'): ?# 小數(shù)點(diǎn)前加0 ? ? ? ? content = '0' + content ? ? if btn in '0123456789': ? ? ? ? content += btn ? ? elif btn == '.': ? ? ? ? lastPart = re.split(r'\+|-|\*|/', content)[-1] ? ? ? ? if '.' in lastPart: ? ? ? ? ? ? tk.messagebox.showerror('錯(cuò)誤', '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('錯(cuò)誤', 'VALUE ERROR') ? ? ? ? ? ? return ? ? elif btn in operators: ? ? ? ? if content.endswith(operators): ? ? ? ? ? ? tk.messagebox.showerror('錯(cuò)誤', '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('錯(cuò)誤', 'Input Error') ? ? ? ? ? ? return ? ? elif btn == '←': ?# 如果按下的是退格‘',則選取當(dāng)前數(shù)字第一位到倒數(shù)第二位 ? ? ? ? content = content[0:-1] ? ? elif btn == '高級(jí)':
其實(shí)此時(shí)已經(jīng)實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的初級(jí)頁(yè)面,能進(jìn)行四則運(yùn)算,并且能輸出相應(yīng)的值。就差高級(jí)這個(gè)接口的實(shí)現(xiàn)啦,接下來(lái)就讓我們來(lái)實(shí)現(xiàn)高級(jí)這個(gè)按鈕的功能吧。
6.低級(jí)頁(yè)面中高級(jí)按鈕的實(shí)現(xiàn)
[注意]:以下代碼全部都是在“高級(jí)”這個(gè)按鈕下來(lái)實(shí)現(xiàn)的,注意python語(yǔ)言的縮進(jìn)。也就是在上述的 elif btn==‘高級(jí)’:之下實(shí)現(xiàn)。
點(diǎn)擊高級(jí)按鈕之后,響應(yīng)對(duì)應(yīng)事件,因?yàn)槲覀冺?yè)面大小不變,所以我們需要調(diào)用btnac.destroy() 來(lái)銷毀低級(jí)頁(yè)面的按鈕,同理調(diào)用:btn1.destroy()、btn2.destroy()等等。也就意味著把初級(jí)頁(yè)面的按鈕全部銷毀,重新布局。此時(shí)有7行按鈕生成。
這里我說(shuō)一下最初的構(gòu)思,本來(lái)想點(diǎn)擊高級(jí)按鈕之后,將原來(lái)的按鈕縮小比例,并且增加新的高級(jí)按鈕。這樣可以實(shí)現(xiàn),而且減少頁(yè)面布局代碼,但是這樣做的同時(shí),初級(jí)界面的那個(gè)‘高級(jí)’按鈕還會(huì)在,曾試圖改變它的名稱,但效果不是很好。并且高級(jí)與初級(jí)頁(yè)面在計(jì)算的時(shí)候都是用的同一種方法,通過(guò)command回調(diào)函數(shù)來(lái)實(shí)現(xiàn)。高級(jí)頁(yè)面需要正則表達(dá)式對(duì)點(diǎn)擊獲取的字符串進(jìn)行處理。所以出現(xiàn)諸多問(wèn)題。因此沒(méi)有用頁(yè)面按鈕比例縮小的那構(gòu)思。而是直接將初級(jí)頁(yè)面的按鈕全部清理,重新布局。這樣就解決了那些問(wèn)題。也完成了高級(jí)<---->低級(jí)頁(yè)面的來(lái)回跳轉(zhuǎn)??雌饋?lái)有種縮放的效果。請(qǐng)讀者自行體會(huì)。
contentEntry.place(x=0, y=45, width=300, height=40) # 重新繪制輸出框 ? ? ? ? # 運(yùn)算符號(hào)按鈕 ? ? ? ? # 第一行 ? ? ? ? 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='低級(jí)', font=('微軟雅黑', 20), fg='orange', bd=0.5, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? command=lambda x='低級(jí)': 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.高級(jí)頁(yè)面的響應(yīng)事件的處理
先來(lái)說(shuō)明一下計(jì)算器計(jì)算某個(gè)表達(dá)式用到的原理:
比如要計(jì)算這個(gè)表達(dá)式:2+3*(sin(0)+lg(10))-2
1.首先用正則表達(dá)式對(duì)這個(gè)字符串處理,計(jì)算出sin(0)=0,字符串變?yōu)椋?+3*(0+lg(10))-2
2.再用正則表達(dá)式處理字符串,計(jì)算出lg(10)=1,字符串變?yōu)椋?+3*(0+1)-2
3.再用正則表達(dá)式處理字符串,計(jì)算括號(hào)(0+1) =1,此時(shí)字符串變成:3+3*1-2
4.四則運(yùn)算直接調(diào)用python自帶的函數(shù)eval()直接計(jì)算結(jié)果。
筆者用此思想來(lái)實(shí)現(xiàn)下述高級(jí)頁(yè)面表達(dá)式的計(jì)算。
【注】:重要實(shí)現(xiàn)的要點(diǎn)說(shuō)明
1.高級(jí)頁(yè)面依然保留初級(jí)頁(yè)面所有布局、功能,在此基礎(chǔ)上增加了很多新功能。
2.在高級(jí)頁(yè)面輸完表達(dá)式,當(dāng)我們點(diǎn)擊‘=’時(shí),計(jì)算某個(gè)表達(dá)式值。用到上述思想。
3.我們以計(jì)算一個(gè)表達(dá)式中含有sin(x)為例來(lái)進(jìn)行簡(jiǎn)單的說(shuō)明:
(1)我們首先判斷sin是不是在某個(gè)表達(dá)式中。
(2)使用正則表達(dá)式:strsin = r’sin(\d+)|sin(-?\d+.\d+)‘來(lái)提取如相應(yīng)的表達(dá)式。
(3) 此時(shí)得到是一個(gè)字符串表達(dá)式’sin(2)’,我們?cè)儆谜齽t表達(dá)式提取里面的數(shù)字value。調(diào)用math庫(kù)里面是sin函數(shù)。來(lái)計(jì)算sin(2), 如:value = str(sin_t(float(value))) 這里的sin_t()是一個(gè)自定義函數(shù)。在下述functions.py文件里面。
(4)此時(shí)我們將計(jì)算得到的數(shù)值,調(diào)用replace() 進(jìn)行替換。就能達(dá)到想要的效果了哦。
別的表達(dá)式都是用的同樣的方法,這里不再贅述。請(qǐng)讀者根據(jù)源代碼自行體會(huì)。
def buttonClick1(btn): ? ? content = contentVar.get() ? ? ? ? ? ? if content.startswith('.'): ?# 小數(shù)點(diǎn)前加0 ? ? ? ? ? ? ? ? content = '0' + content ? ? ? ? ? ? if btn in '0123456789()': ? ? ? ? ? ? ? ? content += btn ? ? ? ? ? ? elif btn == '.': ? ? ? ? ? ? ? ? lastPart = re.split(r'\+|-|\*|/', content)[-1] ? ? ? ? ? ? ? ? if '.' in lastPart: ? ? ? ? ? ? ? ? ? ? tk.messagebox.showerror('錯(cuò)誤', '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('錯(cuò)誤', '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('錯(cuò)誤', '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('錯(cuò)誤', '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('錯(cuò)誤', '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('錯(cuò)誤', '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('錯(cuò)誤', 'VALUE ERROR') ? ? ? ? ? ? ? ? ? ? return ? ? ? ? ? ? elif btn in operators: ? ? ? ? ? ? ? ? if content.endswith(operators): ? ? ? ? ? ? ? ? ? ? tk.messagebox.showerror('錯(cuò)誤', '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 == '←': ?# 如果按下的是退格‘',則選取當(dāng)前數(shù)字第一位到倒數(shù)第二位 ? ? ? ? ? ? ? ? content = content[0:-1]
8.高級(jí)頁(yè)面中低級(jí)按鈕實(shí)現(xiàn)
這里還是用到的同樣思想。對(duì)所有的高級(jí)按鈕全部銷毀。重新布局新的按鈕。這里不再贅述。
elif btn == '低級(jí)': ? ?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='高級(jí)', font=('微軟雅黑', 20), fg='orange', bd=0.5, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? command=lambda x='高級(jí)': 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.運(yùn)行代碼
這里所有的代碼以及完成。最后還差個(gè)functions.py源碼啦。
operators = ('÷', '×', '-', '+', '=', '.') root.mainloop()
10.functions.py實(shí)現(xiàn)
本來(lái)筆者想要用泰勒公式來(lái)計(jì)算復(fù)雜的表達(dá)式。但是沒(méi)有得到想要的結(jié)果。直接調(diào)用math庫(kù)啦,感興趣的讀者可以嘗試用泰勒公式計(jì)算
#!/usr/bin/env python __author__: "Jhze dnowz" import math # 計(jì)算機(jī)倒數(shù) def reciprocal(value): ? ? value = round(float(1) / (float(value)), 10) ? ? return value # 計(jì)算階乘 def factorial(value): ? ? sum = 1 ? ? if value==0 or value==1: ? ? ? ? sum =1 ? ? for i in range(value): ? ? ? ? sum += sum * i ? ? return sum # 計(jì)算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)) # 計(jì)算sin def sin_t(x): ? ? return round(math.sin(x),10) # 計(jì)算cos def cos_t(x): ? ? return round(math.cos(x), 10) # 計(jì)算tan def tan_t(x): ? ? return round(math.tan(x), 10) # 計(jì)算csc def csc_t(x): ? ? return round(float(1)/math.sin(x), 10) # 計(jì)算sec def sec_t(x): ? ? return round(float(1)/math.cos(x), 10) # 計(jì)算lg def lg_t(x): ? ? return round(math.log10(x), 10) # 計(jì)算ln def ln_t(x): ? ? return round(math.log(x, math.e), 10)
11.相關(guān)效果圖
12.動(dòng)態(tài)效果圖
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python制作簡(jiǎn)易計(jì)算器功能
- python制作簡(jiǎn)單計(jì)算器功能
- python實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能
- python tkinter實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- python實(shí)現(xiàn)簡(jiǎn)易版計(jì)算器
- Python實(shí)現(xiàn)簡(jiǎn)單的四則運(yùn)算計(jì)算器
- python 簡(jiǎn)易計(jì)算器程序,代碼就幾行
- 利用Tkinter(python3.6)實(shí)現(xiàn)一個(gè)簡(jiǎn)單計(jì)算器
- 基于python的Tkinter實(shí)現(xiàn)一個(gè)簡(jiǎn)易計(jì)算器
- Pyqt實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能
相關(guān)文章
python開(kāi)發(fā)的自動(dòng)化運(yùn)維工具ansible詳解
ansible是新出現(xiàn)的自動(dòng)化運(yùn)維工具,基于Python開(kāi)發(fā),集合了眾多運(yùn)維工具(puppet、chef、func、fabric)的優(yōu)點(diǎn),實(shí)現(xiàn)了批量系統(tǒng)配置、批量程序部署、批量運(yùn)行命令等功能,這篇文章主要介紹了python開(kāi)發(fā)的自動(dòng)化運(yùn)維工具ansible詳解,需要的朋友可以參考下2021-08-08pycharm中導(dǎo)入模塊錯(cuò)誤時(shí)提示Try to run this command from the system ter
這篇文章主要介紹了pycharm中導(dǎo)入模塊錯(cuò)誤時(shí)提示Try to run this command from the system terminal問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03python利用logging模塊實(shí)現(xiàn)根據(jù)日志級(jí)別打印不同顏色日志的代碼案例
這篇文章主要介紹了python利用logging模塊實(shí)現(xiàn)根據(jù)日志級(jí)別打印不同顏色日志,本文通過(guò)實(shí)例代碼給大家詳細(xì)講解,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12python使用for循環(huán)計(jì)算0-100的整數(shù)的和方法
今天小編就為大家分享一篇python使用for循環(huán)計(jì)算0-100的整數(shù)的和方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02Python seek()和tell()函數(shù)的具體使用
本文主要介紹了Python seek()和tell()函數(shù)的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02python 使用三引號(hào)時(shí)容易犯的小錯(cuò)誤
這篇文章主要介紹了python 使用三引號(hào)時(shí)容易犯的小錯(cuò)誤,幫助新手學(xué)習(xí),避免入坑,感興趣的朋友可以了解下2020-10-10