快速上手,五分鐘內(nèi)完成個性化Python GUI計算器搭建
一、前言
在本教程中,你將學習如何在Python中使用Tkinter在短短幾分鐘內(nèi)制作自己的全功能GUI計算器。
在完成本教程時,除了通常隨Python標準庫一起安裝的Tkinter之外,不需要任何額外的庫。
如果使用的是Linux系統(tǒng),可能需要安裝它:
$ pip install python-tk
一切安裝完畢后,開始編寫我們的計算器代碼,在教程結(jié)束時,將搭建出類似下面的東西:
圖片
二、使用eval()解決數(shù)學問題
eval()是Python中的一個內(nèi)置函數(shù),它會解析表達式參數(shù)并將其作為Python表達式進行求值。
我們將使用eval()的概念來解決數(shù)學表達式。
用法示例:
>>> while True:
... expression = input('Enter equation: ')
... result = eval(expression)
... print(result)
...
Enter equation: 2 + (9/9) *3
5.0
Enter equation: 12 /9 + (18 -2) % 5
2.333333333333333
使用這4行代碼,已經(jīng)在Python中制作了一個命令行計算器,現(xiàn)在讓我們使用相同的概念來制作一個帶有圖形界面的計算器。
這個GUI計算器有三個主要部分:
- 用于顯示表達式的屏幕(框架)
- 保存表達式值的按鈕
- 搭建計算器邏輯
三、為計算器制作一個框架
from tkinter import Tk, Entry, Button, StringVar
class Calculator:
def __init__(self, master):
master.title('Simple Calculator')
master.geometry('360x260+0+0')
master.config(bg='#438')
master.resizable(False, False)
root = Tk()
calculator = Calculator(root)
root.mainloop()
輸出:
圖片
四、添加一個屏幕來顯示表達式
from tkinter import Tk, Entry, Button, StringVar
class Calculator:
def __init__(self, master):
master.title('Simple Calculator')
master.geometry('360x260+0+0')
master.config(bg='#438')
master.resizable(False, False)
self.equation = StringVar()
self.entry_value = ''
Entry(width = 28,bg='lightblue', font = ('Times', 16), textvariable = self.equation).place(x=0,y=0)
root = Tk()
calculator = Calculator(root)
root.mainloop()
輸出:
圖片
如上所示,我們已經(jīng)完成了顯示屏幕的構(gòu)建,現(xiàn)在需要添加一個按鈕用于形成數(shù)學表達式。
五、添加用于形成數(shù)學表達式的按鈕
這些按鈕的創(chuàng)建方式相同,只是它們所存儲的值和它們的位置不同。用于形成數(shù)學表達式的按鈕包括:
- 0到9的數(shù)字
- 數(shù)學運算符+、-、/、%
- 小數(shù)點
- 括號()
我們需要為每個按鈕附加一個命令,以便當我們點擊它時,它就會顯示在顯示屏上。為此,編寫一個簡單的show()函數(shù)來實現(xiàn)這個功能。
from tkinter import Tk, Entry, Button, StringVar
class Calculator:
def __init__(self, master):
master.title('Simple Calculator')
master.geometry('360x260+0+0')
master.config(bg='#438')
master.resizable(False, False)
self.equation = StringVar()
self.entry_value = ''
Entry(width = 28,bg='lightblue', font = ('Times', 16), textvariable = self.equation).place(x=0,y=0)
Button(width=8, text = '(', relief ='flat', command=lambda:self.show('(')).place(x=0,y=50)
Button(width=8, text = ')', relief ='flat', command=lambda:self.show(')')).place(x=90, y=50)
Button(width=8, text = '%', relief ='flat', command=lambda:self.show('%')).place(x=180, y=50)
Button(width=8, text = '1', relief ='flat', command=lambda:self.show(1)).place(x=0,y=90)
Button(width=8, text = '2', relief ='flat', command=lambda:self.show(2)).place(x=90,y=90)
Button(width=8, text = '3', relief ='flat', command=lambda:self.show(3)).place(x=180,y=90)
Button(width=8, text = '4', relief ='flat', command=lambda:self.show(4)).place(x=0,y=130)
Button(width=8, text = '5', relief ='flat', command=lambda:self.show(5)).place(x=90,y=130)
Button(width=8, text = '6', relief ='flat', command=lambda:self.show(6)).place(x=180,y=130)
Button(width=8, text = '7', relief ='flat', command=lambda:self.show(7)).place(x=0,y=170)
Button(width=8, text = '8', relief ='flat', command=lambda:self.show(8)).place(x=180,y=170)
Button(width=8, text = '9', relief ='flat', command=lambda:self.show(9)).place(x=90,y=170)
Button(width=8, text = '0', relief ='flat', command=lambda:self.show(0)).place(x=0,y=210)
Button(width=8, text = '.', relief ='flat', command=lambda:self.show('.')).place(x=90,y=210)
Button(width=8, text = '+', relief ='flat', command=lambda:self.show('+')).place(x=270,y=90)
Button(width=8, text = '-', relief ='flat', command=lambda:self.show('-')).place(x=270,y=130)
Button(width=8, text = '/', relief ='flat', command=lambda:self.show('/')).place(x=270,y=170)
Button(width=8, text = 'x', relief ='flat', command=lambda:self.show('*')).place(x=270,y=210)
def show(self, value):
self.entry_value +=str(value)
self.equation.set(self.entry_value)
root = Tk()
calculator = Calculator(root)
root.mainloop()
輸出:
輸出是一個帶有按鈕的計算器,當你點擊其中任意一個按鈕時,它的值就會顯示在顯示屏上。
現(xiàn)在我們的計算器只剩下兩個按鈕就能完整,一個是重置按鈕用于清除屏幕,另一個是等號(=)按鈕,用于計算表達式并將結(jié)果顯示在屏幕上。
六、為計算器添加重置和等號按鈕
from tkinter import Tk, Entry, Button, StringVar
class Calculator:
def __init__(self, master):
master.title('Simple Calculator')
master.geometry('360x260+0+0')
master.config(bg='#438')
master.resizable(False, False)
self.equation = StringVar()
self.entry_value = ''
Entry(width = 28,bg='lightblue', font = ('Times', 16), textvariable = self.equation).place(x=0,y=0)
Button(width=8, text = '(', relief ='flat', command=lambda:self.show('(')).place(x=0,y=50)
Button(width=8, text = ')', relief ='flat', command=lambda:self.show(')')).place(x=90, y=50)
Button(width=8, text = '%', relief ='flat', command=lambda:self.show('%')).place(x=180, y=50)
Button(width=8, text = '1', relief ='flat', command=lambda:self.show(1)).place(x=0,y=90)
Button(width=8, text = '2', relief ='flat', command=lambda:self.show(2)).place(x=90,y=90)
Button(width=8, text = '3', relief ='flat', command=lambda:self.show(3)).place(x=180,y=90)
Button(width=8, text = '4', relief ='flat', command=lambda:self.show(4)).place(x=0,y=130)
Button(width=8, text = '5', relief ='flat', command=lambda:self.show(5)).place(x=90,y=130)
Button(width=8, text = '6', relief ='flat', command=lambda:self.show(6)).place(x=180,y=130)
Button(width=8, text = '7', relief ='flat', command=lambda:self.show(7)).place(x=0,y=170)
Button(width=8, text = '8', relief ='flat', command=lambda:self.show(8)).place(x=180,y=170)
Button(width=8, text = '9', relief ='flat', command=lambda:self.show(9)).place(x=90,y=170)
Button(width=8, text = '0', relief ='flat', command=lambda:self.show(0)).place(x=0,y=210)
Button(width=8, text = '.', relief ='flat', command=lambda:self.show('.')).place(x=90,y=210)
Button(width=8, text = '+', relief ='flat', command=lambda:self.show('+')).place(x=270,y=90)
Button(width=8, text = '-', relief ='flat', command=lambda:self.show('-')).place(x=270,y=130)
Button(width=8, text = '/', relief ='flat', command=lambda:self.show('/')).place(x=270,y=170)
Button(width=8, text = 'x', relief ='flat', command=lambda:self.show('*')).place(x=270,y=210)
Button(width=8, text = '=', bg='green', relief ='flat', command=self.solve).place(x=180, y=210)
Button(width=8, text = 'AC', relief ='flat', command=self.clear).place(x=270,y=50)
def show(self, value):
self.entry_value +=str(value)
self.equation.set(self.entry_value)
def clear(self):
self.entry_value = ''
self.equation.set(self.entry_value)
def solve(self):
result = eval(self.entry_value)
self.equation.set(result)
root = Tk()
calculator = Calculator(root)
root.mainloop()
輸出:
七、結(jié)語
在短短的五分鐘內(nèi),我們成功地使用Tkinter庫搭建了一個Python GUI計算器。這個計算器可以進行基本的數(shù)學運算,并為用戶提供了友好的交互體驗。
搭建一個GUI計算器不僅僅是一個有趣的項目,它還展示了Python的強大和靈活性。希望對你有所幫助,并激勵你進一步探索和開發(fā)更多有趣的GUI應用程序!