簡易計算器
寫在前面
小編用python實現了一個簡單的計算器,一起來看看吧~
需要環境:
-
pycharm
-
python
一、需求分析
1.1 功能分析
使用Python的Tkinter界面設計實現一個簡單的計算器,主要功能按鈕包括數字鍵、四則運算符、等于號和清除鍵,實現了簡單的加減乘除運算。
1.2 性能分析
① 系統具有易操作性
在日常使用中,這個計算器應該可以滿足大多數用戶的需求,即使在舊計算機上也能夠流暢運行。
② 系統具有高效性
這個計算器應用程序非常簡單,沒有太多的計算和數據處理。因此,它的性能應該是相對較好的,無論是在較老的電腦還是在較新的電腦上都能夠快速響應和運行。
二、技術原理
這個計算器應用程序是基于Tkinter GUI工具集編寫的一個Python應用程序,它使用不同類型的控件和布局管理器來創建圖形用戶界面并實現計算器的各種功能,通過用方法來處理用戶在應用程序上的操作并顯示相應的結果。
三、詳細設計
3.1 導入tkinter庫
① 程序設計
import tkinter as tk
② 程序分析
在程序開頭,通過"import tkinter as tk"導入tkinter庫,以便使用tkinter庫中的控件和方法。
3.2 定義全局變量
① 程序設計
t = ""``color1 = "skyblue"``color2 = "yellow"``color3 = "pink"``color4 = "red"``result = tk.StringVar()
② 程序分析
全局變量t用于表示計算器輸入框中的內容。全局變量result是一個StringVar對象,用于實時更新輸入框中的內容,全局變量color1-4分別代表不同的顏色。
3.3 定義添加函數
① 程序設計
def Add(num):` `global t` `t = t + str(num)` `result.set(t)
② 程序分析
Add函數用于將數字和運算符添加到輸入框中。它獲取單擊的按鈕上的值,將其轉換為字符串并附加到全局變量t中,最后更新輸入框中的內容result。
3.4 定義結果函數
① 程序設計
def Result():` `try:` `global t` `total = str(eval(t))` `result.set(total)` `t = ""` `except:` `result.set(" error ")` `t = ""
② 程序分析
Result函數用于計算表達式并將結果更新到輸入框中。它首先嘗試使用Python的內置eval函數計算從輸入框中獲取的表達式。如果計算成功,則將結果轉換為字符串并設置為result,最后清空全局變量t。如果出現任何錯誤,設置result為" error "字符串并清空全局變量t。
3.5 定義清空函數
① 程序設計
def Clear():` `global t` `t = ""` `result.set("")
② 程序分析
Clear函數用于清空輸入框。
3.6 創建主窗口并指定其大小和位置
① 程序設計
root = tk.Tk()``root.configure(background="black")``root.title("簡易計算器")``sh = root.winfo_screenheight()``sw = root.winfo_screenwidth()``h = 150``w = 295``x = (sw - w) // 2``y = (sh - h) // 2``root.geometry('%dx%d+%d+%d' % (w, h, x, y))
② 程序分析
通過調用tkinter庫中的Tk()函數創建主窗口對象,并設置它的標題和背景顏色和大小。然后通過獲取屏幕高度和寬度來自適應地計算窗口的大小和位置。最后使用geometry()方法將窗口定位和顯示在屏幕上。
3.7 創建輸入框
① 程序設計
entrys = tk.Entry(root, textvariable=result, width=22)``entrys.grid(columnspan=4, ipadx=70)
② 程序分析
創建一個tkinter.Entry對象來顯示計算器的輸入框。并使用grid()方法來將其添加到主窗口上,并設置它的位置和大小。
3.8 創建數字和運算符按鈕
① 程序設計
button1 = tk.Button(root, text=' 1 ', bg=color1, command=lambda: Add(1), height=1, width=7)``button1.grid(row=2, column=0)``button2 = tk.Button(root, text=' 2 ', bg=color1, command=lambda: Add(2), height=1, width=7)``button2.grid(row=2, column=1)``button3 = tk.Button(root, text=' 3 ', bg=color1, command=lambda: Add(3), height=1, width=7)``button3.grid(row=2, column=2)``button4 = tk.Button(root, text=' 4 ', bg=color1, command=lambda: Add(4), height=1, width=7)``button4.grid(row=3, column=0)``button5 = tk.Button(root, text=' 5 ', bg=color1, command=lambda: Add(5), height=1, width=7)``button5.grid(row=3, column=1)``button6 = tk.Button(root, text=' 6 ', bg=color1, command=lambda: Add(6), height=1, width=7)``button6.grid(row=3, column=2)``button7 = tk.Button(root, text=' 7 ', bg=color1, command=lambda: Add(7), height=1, width=7)``button7.grid(row=4, column=0)``button8 = tk.Button(root, text=' 8 ', bg=color1, command=lambda: Add(8), height=1, width=7)``button8.grid(row=4, column=1)``button9 = tk.Button(root, text=' 9 ', bg=color1, command=lambda: Add(9), height=1, width=7)``button9.grid(row=4, column=2)``button0 = tk.Button(root, text=' 0 ', bg=color1, command=lambda: Add(0), height=1, width=7)``button0.grid(row=5, column=0)`` ``add = tk.Button(root, text=' + ', bg=color2, command=lambda: Add("+"), height=1, width=7)``add.grid(row=2, column=3)``subtract = tk.Button(root, text=' - ', bg=color2, command=lambda: Add("-"), height=1, width=7)``subtract.grid(row=3, column=3)``multiply = tk.Button(root, text=' * ', bg=color2, command=lambda: Add("*"), height=1, width=7)``multiply.grid(row=4, column=3)``divide = tk.Button(root, text=' / ', bg=color2, command=lambda: Add("/"), height=1, width=7)``divide.grid(row=5, column=3)
② 程序分析
創建10個數字按鈕和4個運算符按鈕,并用grid()方法將它們添加到主窗口上。每個按鈕都分配了相應的數字或符號,并與Add函數相關聯以便將它們添加到輸入框中。
3.9 創建等于號和清除按鈕
① 程序設計
equal = tk.Button(root, text=' = ', bg=color3, command=Result, height=1, width=7)``equal.grid(row=5, column=2)``clear = tk.Button(root, text='Clear', bg=color4, command=Clear, height=1, width=7)``clear.grid(row=5, column=1)
② 程序分析
為等于號和清除按鈕創建兩個單獨的按鈕,并將它們添加到主窗口上。為等于號按鈕分配Result函數并為清除按鈕分配Clear函數。
四、功能實現
😝除了上面分享,我也給大家整理了Python學習籽料。Python+pycharm安裝包,如果有需要的話,可以V掃描下方二維碼聯系領取哦~
學好 Python 不論是就業還是做副業賺錢都不錯,但要學會 Python 還是要有一個學習規劃。最后大家分享一份全套的 Python 學習資料,給那些想學習 Python 的小伙伴們一點幫助!
因篇幅有限,僅展示部分資料,添加上方即可獲取👆