快速上手,五分鐘內(nèi)完成個(gè)性化Python GUI計(jì)算器搭建
一、前言
在本教程中,你將學(xué)習(xí)如何在Python中使用Tkinter在短短幾分鐘內(nèi)制作自己的全功能GUI計(jì)算器。
在完成本教程時(shí),除了通常隨Python標(biāo)準(zhǔn)庫(kù)一起安裝的Tkinter之外,不需要任何額外的庫(kù)。
如果使用的是Linux系統(tǒng),可能需要安裝它:
$ pip install python-tk
一切安裝完畢后,開始編寫我們的計(jì)算器代碼,在教程結(jié)束時(shí),將搭建出類似下面的東西:
圖片
二、使用eval()解決數(shù)學(xué)問題
eval()是Python中的一個(gè)內(nèi)置函數(shù),它會(huì)解析表達(dá)式參數(shù)并將其作為Python表達(dá)式進(jìn)行求值。
我們將使用eval()的概念來解決數(shù)學(xué)表達(dá)式。
用法示例:
>>> 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中制作了一個(gè)命令行計(jì)算器,現(xiàn)在讓我們使用相同的概念來制作一個(gè)帶有圖形界面的計(jì)算器。
這個(gè)GUI計(jì)算器有三個(gè)主要部分:
- 用于顯示表達(dá)式的屏幕(框架)
- 保存表達(dá)式值的按鈕
- 搭建計(jì)算器邏輯
三、為計(jì)算器制作一個(gè)框架
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()
輸出:
圖片
四、添加一個(gè)屏幕來顯示表達(dá)式
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)在需要添加一個(gè)按鈕用于形成數(shù)學(xué)表達(dá)式。
五、添加用于形成數(shù)學(xué)表達(dá)式的按鈕
這些按鈕的創(chuàng)建方式相同,只是它們所存儲(chǔ)的值和它們的位置不同。用于形成數(shù)學(xué)表達(dá)式的按鈕包括:
- 0到9的數(shù)字
- 數(shù)學(xué)運(yùn)算符+、-、/、%
- 小數(shù)點(diǎn)
- 括號(hào)()
我們需要為每個(gè)按鈕附加一個(gè)命令,以便當(dāng)我們點(diǎn)擊它時(shí),它就會(huì)顯示在顯示屏上。為此,編寫一個(gè)簡(jiǎn)單的show()函數(shù)來實(shí)現(xiàn)這個(gè)功能。
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()
輸出:
輸出是一個(gè)帶有按鈕的計(jì)算器,當(dāng)你點(diǎn)擊其中任意一個(gè)按鈕時(shí),它的值就會(huì)顯示在顯示屏上。
現(xiàn)在我們的計(jì)算器只剩下兩個(gè)按鈕就能完整,一個(gè)是重置按鈕用于清除屏幕,另一個(gè)是等號(hào)(=)按鈕,用于計(jì)算表達(dá)式并將結(jié)果顯示在屏幕上。
六、為計(jì)算器添加重置和等號(hào)按鈕
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庫(kù)搭建了一個(gè)Python GUI計(jì)算器。這個(gè)計(jì)算器可以進(jìn)行基本的數(shù)學(xué)運(yùn)算,并為用戶提供了友好的交互體驗(yàn)。
搭建一個(gè)GUI計(jì)算器不僅僅是一個(gè)有趣的項(xiàng)目,它還展示了Python的強(qiáng)大和靈活性。希望對(duì)你有所幫助,并激勵(lì)你進(jìn)一步探索和開發(fā)更多有趣的GUI應(yīng)用程序!