Python GUI Calculator Project:
We can use the PyCharm code editor for this example. If you do not know about it then follow this link- How to install PyCharm for Python and create a program in it. |
(1) Open the PyCharm code editor and create a new project in it. We named the project calculator.py. To build a GUI Calculator in Python we need to install the package. For this open the terminal in PyCharm and install one package i.e. tkinter. You install this package by simply typing in the terminal following command i.e. pip install tk and then press enter as shown below.
Note: If the package shows an error while using in the code, you can simply install the package by clicking on the red bulb which appears below the error and you can directly install the package from here.
(2) Now write the following code for creating Python GUI Calculator Project as shown below.
Python GUI Calculator Project Source Code:
import tkinter
calculation = ""
def add_to_calculation(symbol):
global calculation
calculation += str(symbol)
txt_result.delete(1.0,"end")
txt_result.insert(1.0,calculation)
def evaluate_calculation():
global calculation
try:
calculation = str(eval(calculation))
txt_result.delete(1.0,"end")
txt_result.insert(1.0, calculation)
except:
clear_field()
txt_result.insert(1.0,"Error...")
def clear_field():
global calculation
calculation = ""
txt_result.delete(1.0,"end")
cal_window = tkinter.Tk()
cal_window.geometry("400x280")
cal_window.title("Simple GUI Calculator")
cal_window.config(bg="black")
cal_window.resizable(False,False)
txt_result = tkinter.Text(cal_window, height=2, width=27,
font=("Open Sans",20),bg="white",fg="black")
txt_result.grid(columnspan=6)
button1 = tkinter.Button(cal_window, text="1", command=lambda: add_to_calculation(1),
width=6,font=("Open Sans",16),bg="black",fg="white")
button1.grid(row=2,column=1)
button2 = tkinter.Button(cal_window, text="2", command=lambda: add_to_calculation(2),
width=6,font=("Open Sans",16),bg="black",fg="white")
button2.grid(row=2,column=2)
button3 = tkinter.Button(cal_window, text="3", command=lambda: add_to_calculation(3),
width=6,font=("Open Sans",16),bg="black",fg="white")
button3.grid(row=2,column=3)
button4 = tkinter.Button(cal_window, text="4", command=lambda: add_to_calculation(4),
width=6,font=("Open Sans",16),bg="black",fg="white")
button4.grid(row=3,column=1)
button5 = tkinter.Button(cal_window, text="5", command=lambda: add_to_calculation(5),
width=6,font=("Open Sans",16),bg="black",fg="white")
button5.grid(row=3,column=2)
button6 = tkinter.Button(cal_window, text="6", command=lambda: add_to_calculation(6),
width=6,font=("Open Sans",16),bg="black",fg="white")
button6.grid(row=3,column=3)
button7 = tkinter.Button(cal_window, text="7", command=lambda: add_to_calculation(7),
width=6,font=("Open Sans",16),bg="black",fg="white")
button7.grid(row=4,column=1)
button8 = tkinter.Button(cal_window, text="8", command=lambda: add_to_calculation(8),
width=6,font=("Open Sans",16),bg="black",fg="white")
button8.grid(row=4,column=2)
button9 = tkinter.Button(cal_window, text="9", command=lambda: add_to_calculation(9),
width=6,font=("Open Sans",16),bg="black",fg="white")
button9.grid(row=4,column=3)
button0 = tkinter.Button(cal_window, text="0", command=lambda: add_to_calculation(0),
width=6,font=("Open Sans",16),bg="black",fg="white")
button0.grid(row=5,column=2)
button_plus = tkinter.Button(cal_window, text="+", command=lambda: add_to_calculation("+"),
width=6,font=("Open Sans",16),bg="#F9B81D",fg="black")
button_plus.grid(row=2,column=4)
button_minus = tkinter.Button(cal_window, text="-", command=lambda: add_to_calculation("-"),
width=6,font=("Open Sans",16),bg="#F9B81D",fg="black")
button_minus.grid(row=3,column=4)
button_multiplication = tkinter.Button(cal_window, text="*", command=lambda: add_to_calculation("*"),
width=6,font=("Open Sans",16),bg="#F9B81D",fg="black")
button_multiplication.grid(row=4,column=4)
button_division = tkinter.Button(cal_window, text="/", command=lambda: add_to_calculation("/"),
width=6,font=("Open Sans",16),bg="#F9B81D",fg="black")
button_division.grid(row=5,column=4)
button_openpar = tkinter.Button(cal_window, text="(", command=lambda: add_to_calculation("("),
width=6,font=("Open Sans",16),bg="#F9B81D",fg="black")
button_openpar.grid(row=5,column=1)
button_closepar = tkinter.Button(cal_window, text=")", command=lambda: add_to_calculation(")"),
width=6,font=("Open Sans",16),bg="#F9B81D",fg="black")
button_closepar.grid(row=5,column=3)
button_equals = tkinter.Button(cal_window, text="=", command=evaluate_calculation,
width=14,font=("Open Sans",16),bg="#F9B81D",fg="black")
button_equals.grid(row=6,column=3,columnspan=2)
button_clear = tkinter.Button(cal_window, text="C", command=clear_field,
width=14,font=("Open Sans",16),bg="#F9B81D",fg="black")
button_clear.grid(row=6,column=1,columnspan=2)
cal_window.mainloop()
(3) After running the code, the output window will pop us as shown below.
Comments (No)