日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Python?tkinter實現計算器功能_python

作者:Jhze ? 更新時間: 2022-04-10 編程語言

本文實例為大家分享了Python tkinter實現計算器功能的具體代碼,供大家參考,具體內容如下

python版本: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.動態效果圖

原文鏈接:https://blog.csdn.net/sinat_38812250/article/details/89355216

欄目分類
最近更新