0% found this document useful (0 votes)
16 views2 pages

Calculator

This document contains a Python script that implements a simple graphical calculator using the Tkinter library. The calculator supports basic arithmetic operations and allows input through both buttons and keyboard keys. It features an entry field for displaying expressions and results, along with error handling for invalid expressions.

Uploaded by

Raihan Ansari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views2 pages

Calculator

This document contains a Python script that implements a simple graphical calculator using the Tkinter library. The calculator supports basic arithmetic operations and allows input through both buttons and keyboard keys. It features an entry field for displaying expressions and results, along with error handling for invalid expressions.

Uploaded by

Raihan Ansari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import tkinter as tk

from tkinter import messagebox

class Calculator:
def __init__(self, root):
self.root = root
self.root.title("Calculator")
self.root.geometry("400x600")
self.root.resizable(False, False)

self.expression = ""
self.input_text = tk.StringVar()

self.root.configure(bg="#1e1e1e")

display_frame = tk.Frame(self.root, bg="#1e1e1e")


display_frame.pack(expand=True, fill="both")

display_field = tk.Entry(
display_frame, textvariable=self.input_text, font=("Arial", 24),
justify="right", bd=10, bg="#333333", fg="white"
)
display_field.pack(expand=True, fill="both", padx=10, pady=20)

button_frame = tk.Frame(self.root, bg="#1e1e1e")


button_frame.pack(expand=True, fill="both")

buttons = [
['7', '8', '9', '/'],
['4', '5', '6', '*'],
['1', '2', '3', '-'],
['C', '0', '=', '+']
]

for row in buttons:


row_frame = tk.Frame(button_frame, bg="#1e1e1e")
row_frame.pack(expand=True, fill="both")
for button_text in row:
button = tk.Button(
row_frame, text=button_text, font=("Arial", 18), width=3,
height=2,
fg="white", bg="#4b4b4b", activebackground="#5c5c5c",
borderwidth=0,
command=lambda text=button_text: self.on_button_click(text)
)
button.pack(side="left", expand=True, fill="both", padx=5, pady=5)

self.root.bind("<Key>", self.on_key_press)

def on_button_click(self, text):


if text == "=":
self.evaluate_expression()
elif text == "C":
self.clear_expression()
else:
self.expression += str(text)
self.input_text.set(self.expression)

def on_key_press(self, event):


if event.char.isdigit() or event.char in "+-*/":
self.expression += event.char
self.input_text.set(self.expression)
elif event.keysym == "Return":
self.evaluate_expression()
elif event.keysym == "BackSpace":
self.expression = self.expression[:-1]
self.input_text.set(self.expression)
elif event.char.lower() == "c":
self.clear_expression()

def evaluate_expression(self):
try:
result = str(eval(self.expression))
self.input_text.set(result)
self.expression = result
except Exception as e:
messagebox.showerror("Error", "Invalid Expression")
self.input_text.set("")
self.expression = ""

def clear_expression(self):
self.expression = ""
self.input_text.set("")

if __name__ == "__main__":
root = tk.Tk()
calc = Calculator(root)
root.mainloop()

You might also like