These are the codes for a GUI based simple calculator written in Python.
Paste the following codes in your python code editor: [The code below is created with PyCharm Community Edition and tested working]
import keyboard
import tkinter
import tkinter as tk
from tkinter import *
from tkinter import ttk
calculation = ""
expression = ""
def add_to_calculation(symbol):
global calculation
if symbol == "rem":
calculation = calculation[:-1]
elif symbol == "=":
evaulate_calculation()
else:
calculation += str(symbol)
text_result.delete(1.0, "end")
text_result.insert(1.0, calculation)
def evaulate_calculation():
global calculation
expression = text_result.get("1.0", "end-1c")
try:
calculation = str(eval(expression))
text_result.delete(1.0, "end")
text_result.insert(1.0, calculation)
except:
clear_canvas()
text_result.tag_configure("error", foreground="red")
text_result.insert(1.0, "Syntax Error", "error")
pass
def clear_canvas():
global calculation
calculation = ""
text_result.delete(1.0, "end")
def handle_enter(event):
# Prevent the default behavior of adding a newline
text_result.insert("insert", "Enter pressed\n")
evaulate_calculation()
pass # Stop further event handling
def handle_keypress(event):
if event.keysym == "KP_1" or event.keysym == "1":
add_to_calculation(1)
elif event.keysym == "KP_2" or event.keysym == "2":
add_to_calculation(2)
elif event.keysym == "KP_3" or event.keysym == "3":
add_to_calculation(3)
elif event.keysym == "KP_4" or event.keysym == "4":
add_to_calculation(4)
elif event.keysym == "KP_5" or event.keysym == "5":
add_to_calculation(5)
elif event.keysym == "KP_6" or event.keysym == "6":
add_to_calculation(6)
elif event.keysym == "KP_7" or event.keysym == "7":
add_to_calculation(7)
elif event.keysym == "KP_8" or event.keysym == "8":
add_to_calculation(8)
elif event.keysym == "KP_9" or event.keysym == "9":
add_to_calculation(9)
elif event.keysym == "KP_0" or event.keysym == "0":
add_to_calculation(0)
elif event.keysym == "KP_Add" or event.char == "+":
add_to_calculation("+")
elif event.keysym == "minus" or event.char == "-":
add_to_calculation("-")
elif event.keysym == "slash" or event.char == "/":
add_to_calculation("/")
elif event.keysym == "asterisk" or event.char == "*":
add_to_calculation("*")
elif event.keysym == "equal" or event.char == "=":
add_to_calculation("=")
elif event.keysym == "-enter":
add_to_calculation("=")
else:
pass
root = tk.Tk()
style = ttk.Style()
root.geometry("563x565+100+200")
root.configure(background="#1C1C1C")
root.resizable(False, False)
#Images REF
img0= PhotoImage(file=f"image for number 0.png")
img1= PhotoImage(file=f"image for number 1.png")
import tkinter
import tkinter as tk
from tkinter import *
from tkinter import ttk
calculation = ""
expression = ""
def add_to_calculation(symbol):
global calculation
if symbol == "rem":
calculation = calculation[:-1]
elif symbol == "=":
evaulate_calculation()
else:
calculation += str(symbol)
text_result.delete(1.0, "end")
text_result.insert(1.0, calculation)
def evaulate_calculation():
global calculation
expression = text_result.get("1.0", "end-1c")
try:
calculation = str(eval(expression))
text_result.delete(1.0, "end")
text_result.insert(1.0, calculation)
except:
clear_canvas()
text_result.tag_configure("error", foreground="red")
text_result.insert(1.0, "Syntax Error", "error")
pass
def clear_canvas():
global calculation
calculation = ""
text_result.delete(1.0, "end")
def handle_enter(event):
# Prevent the default behavior of adding a newline
text_result.insert("insert", "Enter pressed\n")
evaulate_calculation()
pass # Stop further event handling
def handle_keypress(event):
if event.keysym == "KP_1" or event.keysym == "1":
add_to_calculation(1)
elif event.keysym == "KP_2" or event.keysym == "2":
add_to_calculation(2)
elif event.keysym == "KP_3" or event.keysym == "3":
add_to_calculation(3)
elif event.keysym == "KP_4" or event.keysym == "4":
add_to_calculation(4)
elif event.keysym == "KP_5" or event.keysym == "5":
add_to_calculation(5)
elif event.keysym == "KP_6" or event.keysym == "6":
add_to_calculation(6)
elif event.keysym == "KP_7" or event.keysym == "7":
add_to_calculation(7)
elif event.keysym == "KP_8" or event.keysym == "8":
add_to_calculation(8)
elif event.keysym == "KP_9" or event.keysym == "9":
add_to_calculation(9)
elif event.keysym == "KP_0" or event.keysym == "0":
add_to_calculation(0)
elif event.keysym == "KP_Add" or event.char == "+":
add_to_calculation("+")
elif event.keysym == "minus" or event.char == "-":
add_to_calculation("-")
elif event.keysym == "slash" or event.char == "/":
add_to_calculation("/")
elif event.keysym == "asterisk" or event.char == "*":
add_to_calculation("*")
elif event.keysym == "equal" or event.char == "=":
add_to_calculation("=")
elif event.keysym == "-enter":
add_to_calculation("=")
else:
pass
root = tk.Tk()
style = ttk.Style()
root.geometry("563x565+100+200")
root.configure(background="#1C1C1C")
root.resizable(False, False)
#Images REF
img0= PhotoImage(file=f"image for number 0.png")
img1= PhotoImage(file=f"image for number 1.png")
img2=PhotoImage(file=f"image for number 2.png")
img3=PhotoImage(file=f"image for number 3.png")
img4=PhotoImage(file=f"image for number 4.png")
img5=PhotoImage(file=f"image for number 5.png")
img6=PhotoImage(file=f"image for number 6.png")
img7=PhotoImage(file=f"image for number 7.png")
img8=PhotoImage(file=f"image for number 8.png")
img9=PhotoImage(file=f"image for number 9.png")
img_plus=PhotoImage(file=f"image for sign +.png")
img_minus=PhotoImage(file=f"image for sign -.png")
img_div=PhotoImage(file=f"image for sign div.png")
img_mul=PhotoImage(file=f"image for sign *.png")
img_Lpara=PhotoImage(file=f"image for sign (.png")
img_Rpara=PhotoImage(file=f"image for sign ).png")
img_clear=PhotoImage(file=f"image for sign C.png")
img_rem=PhotoImage(file=f"image for sign remove.png")
img_equals=PhotoImage(file=f"image for sign =.png")
#curved buttons
style.configure("TButton", relief="raised", borderwidth=0, width=0, padding=0,
font=("Arial", 14), background="#1C1C1C")
style.configure("Enter.TButton", relief="raised", borderwidth=3, width=0,
style.configure("Enter.TButton", relief="raised", borderwidth=3, width=0,
font=("Arial", 14), background="#1C1C1C", padding=1)
tk.Text(background="#434343")
style.map("TButton", foreground=[('active', '#1C1C1C')],
tk.Text(background="#434343")
style.map("TButton", foreground=[('active', '#1C1C1C')],
background=[('active', '#1C1C1')], highlightcolor=[('focus', '#1C1C1')])
style.map('Enter.TButton', background=[('active', '#1C1C1C'), ('pressed', 'yellow')])
text_result = tk.Text(root, height=2, width=33, font=("Arial", 24))
text_result.grid(columnspan=15)
text_result.configure(background="#1C1C1C", foreground="white")
#keybinds for hardware input
root.bind("<KeyPress>", handle_keypress)
btn_1 = ttk.Button(root, image=img1, text="1", command=lambda: add_to_calculation(1),
style.map('Enter.TButton', background=[('active', '#1C1C1C'), ('pressed', 'yellow')])
text_result = tk.Text(root, height=2, width=33, font=("Arial", 24))
text_result.grid(columnspan=15)
text_result.configure(background="#1C1C1C", foreground="white")
#keybinds for hardware input
root.bind("<KeyPress>", handle_keypress)
btn_1 = ttk.Button(root, image=img1, text="1", command=lambda: add_to_calculation(1),
style="TButton")
btn_1.grid(row=2, column=1)
btn_2 = ttk.Button(root, image=img2, text="2", command=lambda: add_to_calculation(2),
btn_1.grid(row=2, column=1)
btn_2 = ttk.Button(root, image=img2, text="2", command=lambda: add_to_calculation(2),
style="TButton")
btn_2.grid(row=2, column=2)
btn_3 = ttk.Button(root,image=img3, text="3", command=lambda: add_to_calculation(3),
btn_2.grid(row=2, column=2)
btn_3 = ttk.Button(root,image=img3, text="3", command=lambda: add_to_calculation(3),
style="TButton")
btn_3.grid(row=2, column=3)
btn_4 = ttk.Button(root,image=img4, text="4", command=lambda: add_to_calculation(4),
btn_3.grid(row=2, column=3)
btn_4 = ttk.Button(root,image=img4, text="4", command=lambda: add_to_calculation(4),
style="TButton")
btn_4.grid(row=3, column=1)
btn_5 = ttk.Button(root,image=img5, text="5", command=lambda: add_to_calculation(5),
btn_4.grid(row=3, column=1)
btn_5 = ttk.Button(root,image=img5, text="5", command=lambda: add_to_calculation(5),
style="TButton")
btn_5.grid(row=3, column=2)
btn_6 = ttk.Button(root,image=img6, text="6", command=lambda: add_to_calculation(6),
btn_5.grid(row=3, column=2)
btn_6 = ttk.Button(root,image=img6, text="6", command=lambda: add_to_calculation(6),
style="TButton")
btn_6.grid(row=3, column=3)
btn_7 = ttk.Button(root,image=img7, text="7", command=lambda: add_to_calculation(7),
btn_6.grid(row=3, column=3)
btn_7 = ttk.Button(root,image=img7, text="7", command=lambda: add_to_calculation(7),
style="TButton")
btn_7.grid(row=4, column=1)
btn_8 = ttk.Button(root,image=img8, text="8", command=lambda: add_to_calculation(8),
btn_7.grid(row=4, column=1)
btn_8 = ttk.Button(root,image=img8, text="8", command=lambda: add_to_calculation(8),
style="TButton")
btn_8.grid(row=4, column=2)
btn_9 = ttk.Button(root,image=img9, text="9", command=lambda: add_to_calculation(9),
btn_8.grid(row=4, column=2)
btn_9 = ttk.Button(root,image=img9, text="9", command=lambda: add_to_calculation(9),
style="TButton")
btn_9.grid(row=4, column=3)
btn_0 = ttk.Button(root,image=img0, text="0", command=lambda: add_to_calculation(0),
btn_9.grid(row=4, column=3)
btn_0 = ttk.Button(root,image=img0, text="0", command=lambda: add_to_calculation(0),
style="TButton")
btn_0.grid(row=5, column=2)
btn_mul = ttk.Button(root,image=img_mul, text="X", command=lambda: add_to_calculation("*"),
btn_0.grid(row=5, column=2)
btn_mul = ttk.Button(root,image=img_mul, text="X", command=lambda: add_to_calculation("*"),
style="TButton")
btn_mul.grid(row=5, column=1)
btn_divi = ttk.Button(root,image=img_div, text="/", command=lambda: add_to_calculation("/"),
btn_mul.grid(row=5, column=1)
btn_divi = ttk.Button(root,image=img_div, text="/", command=lambda: add_to_calculation("/"),
style="TButton")
btn_divi.grid(row=5, column=3)
btn_plus = ttk.Button(root,image=img_plus, text="+", command=lambda: add_to_calculation("+"),
btn_divi.grid(row=5, column=3)
btn_plus = ttk.Button(root,image=img_plus, text="+", command=lambda: add_to_calculation("+"),
style="TButton")
btn_plus.grid(row=3, column=5)
btn_min = ttk.Button(root,image=img_minus, text="-", command=lambda: add_to_calculation("-"),
btn_plus.grid(row=3, column=5)
btn_min = ttk.Button(root,image=img_minus, text="-", command=lambda: add_to_calculation("-"),
style="TButton")
btn_min.grid(row=2, column=5)
btn_equals = ttk.Button(root,image=img_equals, text="=",
btn_min.grid(row=2, column=5)
btn_equals = ttk.Button(root,image=img_equals, text="=",
command=lambda: evaulate_calculation(), style="Enter.TButton")
btn_equals.grid(row=4, column=5, rowspan=2)
btn_remove = ttk.Button(root,image=img_rem, text="⬅️",
btn_remove = ttk.Button(root,image=img_rem, text="⬅️",
command=lambda: add_to_calculation("rem"), style="TButton")
btn_remove.grid(row=6, column=5)
btn_clear = ttk.Button(root,image=img_clear, text="C",
btn_remove.grid(row=6, column=5)
btn_clear = ttk.Button(root,image=img_clear, text="C",
command=clear_canvas, style="TButton")
btn_clear.grid(row=6, column=3)
btn_Rparanthesis = ttk.Button(root,image=img_Rpara, text=")",
btn_clear.grid(row=6, column=3)
btn_Rparanthesis = ttk.Button(root,image=img_Rpara, text=")",
command=lambda: add_to_calculation(")"), style="TButton")
btn_Rparanthesis.grid(row=6, column=2)
btn_Lparanthesis = ttk.Button(root,image=img_Lpara, text="(",
btn_Rparanthesis.grid(row=6, column=2)
btn_Lparanthesis = ttk.Button(root,image=img_Lpara, text="(",
command=lambda: add_to_calculation("("), style="TButton")
btn_Lparanthesis.grid(row=6, column=1)
#maintains the UI, so everything should be implemented before "root.mainloop()"
root.mainloop()
btn_Lparanthesis.grid(row=6, column=1)
#maintains the UI, so everything should be implemented before "root.mainloop()"
root.mainloop()
#If you encounter any problems in the above code just contact me at "aloknathjha77@gmail.com".
#I will respond in 2-3 business days.
#Thank You For Visiting!
Post a Comment
Please leave a review