如何使用Python中的tkinter构建简单的GUI计算器
介绍
在Python中,我们使用 tkinter库 来创建GUI组件并构建更好的用户界面。
在本文中,我们将学习构建基于GUI的简单计算器应用程序的方法。
入门
在我们开始之前,我们需要先进行一些组织。
让我们首先下载Python的图像处理库,我们将使用它来从我们的本地系统获取图像。要安装PIL(Pillow),请启动终端并输入以下命令。
现在您已经安装了软件包。您将需要下载计算器所需的图标。
您可以在Google图像上下载所需的图标。但是,如果您想使用我为此项目使用的相同一组图标,则可以在以下位置下载:
https://www.dropbox.com/sh/0zqd6zd9b8asmor/AAC3d2iOvMRl8INkbCuMUo_ya?dl=0.
确保您将所有图标下载到名为“asset”的文件夹中。
接下来,我们需要导入所需的模块。
就是这样。您现在应该已经准备好开始了。
创建函数
首先,我们必须创建GUI组件将使用的函数。
有三个主要函数,一个是在按下数字或符号时,另一个是在按下等于按钮时,最后一个是在按下清除按钮时。
让我们首先初始化一些全局变量−
示例
当按下数字键时的函数−
示例1
当按下等于按钮时的函数−
当按下清除按钮时的函数−
示例
现在,我们已经定义了函数,我们可以开始主函数并开始处理GUI组件了。
上面的代码将结构出一个完美的计算器。
注意 −为了避免出现错误,请确保按照上面的代码精确地遵循文件结构。将标志图标保存在assets文件夹中的Calculator文件夹内。
请按照以下格式进行操作−
接下来,让我们设计文本字段,以便我们可以看到数字。
接下来,我们将按照一个接一个的方式向GUI窗口添加图标。以下是一个简单的示例,按照它进行操作或者从本文末尾的完整代码中复制。
示例
类似上述行,依次为button2,button3等按钮操作,直到涵盖所有数字和符号。
就是这样了。如果您现在运行程序,您将看到一个非常抽象的计算器。
如果您无法跟进,可以从下面复制完整代码。
示例
Sorry, I cannot perform this task as the HTML English text was not provided. Please provide the text and I will be happy to translate it for you.
“`python
<pre><code> from Tkinter import *
from PIL import Image
from PIL import ImageTk
txt = ""
res = False
ans = 0
def press(num):
global txt, ans, res
if (res==True):
txt = ans
res = False
txt = txt + str(num)
equation.set(txt)
def equal():
try:
global txt, ans, res
ans = str(eval(txt))
equation.set(ans)
res = True
except:
equation.set("ERROR : Invalid Equation")
txt=""
def clear():
global txt, ans, res
txt = ""
equation.set("")
res = False
if __name__ == "__main__":
window = Tk()
window.configure(background="black")
window.title("Calculator")
window.iconbitmap("assets\Calculator\Logo.ico")
window.geometry("343×417")
window.resizable(0,0)
equation = StringVar()
txt_field = Entry(relief=RIDGE,textvariable=equation,bd=10,font=("Aerial",20),bg="powder blue")
txt_field.grid(columnspan=4,ipady=10,ipadx=10,sticky="nsew")
width=80
height=80
img1 = Image.open("assets/Calculator/one.PNG")
img1 = img1.resize((width,height))
oneImage = ImageTk.PhotoImage(img1)
button1 = Button(window, image=oneImage,bg="white",command = lambda:press(1),height=height,width=width)
button1.grid(row=2,column=0,sticky="nsew")
img2 = Image.open("assets/Calculator/two.PNG")
img2 = img2.resize((width,height))
twoImage = ImageTk.PhotoImage(img2)
button2 = Button(window, image=twoImage,bg="white",command = lambda:press(2),height=height,width=width)
button2.grid(row=2,column=1,sticky="nsew")
img3 = Image.open("assets/Calculator/three.PNG")
img3 = img3.resize((width,height))
threeImage = ImageTk.PhotoImage(img3)
button3 = Button(window, image=threeImage,bg="white",command = lambda:press(3),height=height,width=width)
button3.grid(row=2,column=2,sticky="nsew")
img4 = Image.open("assets/Calculator/four.PNG")
img4 = img4.resize((width,height))
fourImage = ImageTk.PhotoImage(img4)
button4 = Button(window, image=fourImage,bg="white",command = lambda:press(4),height=height,width=width)
button4.grid(row=3,column=0,sticky="nsew")
img5 = Image.open("assets/Calculator/five.PNG")
img5 = img5.resize((width,height))
fiveImage = ImageTk.PhotoImage(img5)
button5 = Button(window, image=fiveImage,bg="white",command = lambda:press(5),height=height,width=width)
button5.grid(row=3,column=1,sticky="nsew")
img6 = Image.open("assets/Calculator/six.PNG")
img6 = img6.resize((width,height))
sixImage = ImageTk.PhotoImage(img6)
button6 = Button(window, image=sixImage,bg="white",command = lambda:press(6),height=height,width=width)
button6.grid(row=3,column=2,sticky="nsew")
img7 = Image.open("assets/Calculator/seven.PNG")
img7 = img7.resize((width,height))
sevenImage = ImageTk.PhotoImage(img7)
button7 = Button(window, image=sevenImage,bg="white",command = lambda:press(7),height=height,width=width)
button7.grid(row=4,column=0,sticky="nsew")
img8 = Image.open("assets/Calculator/eight.PNG")
img8 = img8.resize((width,height))
eightImage = ImageTk.PhotoImage(img8)
button8 = Button(window, image=eightImage,bg="white",command = lambda:press(8),height=height,width=width)
button8.grid(row=4,column=1,sticky="nsew")
img9 = Image.open("assets/Calculator/nine.PNG")
img9 = img9.resize((width,height))
nineImage = ImageTk.PhotoImage(img9)
button9 = Button(window, image=nineImage,bg="white",command = lambda:press(9),height=height,width=width)
button9.grid(row=4,column=2,sticky="nsew")
img0 = Image.open("assets/Calculator/zero.PNG")
img0 = img0.resize((width,height))
zeroImage = ImageTk.PhotoImage(img0)
button0 = Button(window, image=zeroImage,bg="white",command = lambda:press(0),height=height,width=width)
button0.grid(row=5,column=1,sticky="nsew")
imgx = Image.open("assets/Calculator/multiply.PNG")
imgx = imgx.resize((width,height))
multiplyImage = ImageTk.PhotoImage(imgx)
buttonx = Button(window, image=multiplyImage,bg="white",command = lambda:press("*"),height=height,width=width)
buttonx.grid(row=2,column=3,sticky="nsew")
imge = Image.open("assets/Calculator/equals.PNG")
imge = imge.resize((width,height))
equalsImage = ImageTk.PhotoImage(imge)
buttonEqual = Button(window, image=equalsImage,bg="white",command = equal,height=height,width=width)
buttonEqual.grid(row=5,column=2,sticky="nsew")
imgc = Image.open("assets/Calculator/clear.PNG")
imgc = imgc.resize((width,height))
clearImage = ImageTk.PhotoImage(imgc)
buttonClear = Button(window, image=clearImage,bg="white",command = clear,height=height,width=width)
buttonClear.grid(row=5,column=0,sticky="nsew")
imgp = Image.open("assets/Calculator/plus.PNG")
imgp = imgp.resize((width,height))
plusImage = ImageTk.PhotoImage(imgp)
buttonPlus = Button(window, image=plusImage,bg="white",command = lambda:press("+"),height=height,width=width)
buttonPlus.grid(row=3,column=3,sticky="nsew")
imgm = Image.open("assets/Calculator/minus.PNG")
imgm = imgm.resize((width,height))
minusImage = ImageTk.PhotoImage(imgm)
buttonMinus = Button(window, image=minusImage,bg="white",command = lambda:press("-"),height=height,width=width)
buttonMinus.grid(row=4,column=3,sticky="nsew")
imgd = Image.open("assets/Calculator/divide.PNG")
imgd = imgd.resize((width,height))
divideImage = ImageTk.PhotoImage(imgd)
buttonDivide = Button(window, image=divideImage,bg="white",command = lambda:press("/"),height=height,width=width)
buttonDivide.grid(row=5,column=3,sticky="nsew")
window.mainloop()
“`
“`html 7 8 9 ÷ 4 5 6 x 1 2 3 – . 0 + = AC “`
如果您对上述程序具有格式问题,您可以从https://github.com/SVijayB/PyHub/blob/master/Graphics/Simple%20Calculator.py获取它。