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

Code Word For Scientific Calculator

This document contains a Python script for a simple scientific calculator using the Tkinter library. It includes functions for basic arithmetic operations, square, and square root, along with a graphical user interface that allows users to input numbers and operations. The calculator handles errors gracefully by displaying 'Error' when invalid inputs are provided.

Uploaded by

Sohaib Ahmed
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

Code Word For Scientific Calculator

This document contains a Python script for a simple scientific calculator using the Tkinter library. It includes functions for basic arithmetic operations, square, and square root, along with a graphical user interface that allows users to input numbers and operations. The calculator handles errors gracefully by displaying 'Error' when invalid inputs are provided.

Uploaded by

Sohaib Ahmed
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

import math

def button_click(number):
current = entry.get()
entry.delete(0, tk.END)
entry.insert(tk.END, current + str(number))

def button_clear():
entry.delete(0, tk.END)

def button_equal():
expression = entry.get()
try:
result = eval(expression)
entry.delete(0, tk.END)
entry.insert(tk.END, result)
except:
entry.delete(0, tk.END)
entry.insert(tk.END, "Error")

def square_root():
try:
num = float(entry.get())
result = math.sqrt(num)
entry.delete(0, tk.END)
entry.insert(tk.END, result)
except:
entry.delete(0, tk.END)
entry.insert(tk.END, "Error")

def square():
try:
num = float(entry.get())
result = num ** 2
entry.delete(0, tk.END)
entry.insert(tk.END, result)
except:
entry.delete(0, tk.END)
entry.insert(tk.END, "Error")

# Create the main window


window = tk.Tk()
window.title("Scientific Calculator")

# Create an entry widget for the display


entry = tk.Entry(window, width=30)
entry.grid(row=0, column=0, columnspan=5)

# Create number buttons


button_1 = tk.Button(window, text="1", padx=20, pady=10, command=lambda:
button_click(1))
button_2 = tk.Button(window, text="2", padx=20, pady=10, command=lambda:
button_click(2))
button_3 = tk.Button(window, text="3", padx=20, pady=10, command=lambda:
button_click(3))
button_4 = tk.Button(window, text="4", padx=20, pady=10, command=lambda:
button_click(4))
button_5 = tk.Button(window, text="5", padx=20, pady=10, command=lambda:
button_click(5))
button_6 = tk.Button(window, text="6", padx=20, pady=10, command=lambda:
button_click(6))
button_7 = tk.Button(window, text="7", padx=20, pady=10, command=lambda:
button_click(7))
button_8 = tk.Button(window, text="8", padx=20, pady=10, command=lambda:
button_click(8))
button_9 = tk.Button(window, text="9", padx=20, pady=10, command=lambda:
button_click(9))
button_0 = tk.Button(window, text="0", padx=20, pady=10, command=lambda:
button_click(0))

# Create operator buttons


button_add = tk.Button(window, text="+", padx=20, pady=10, command=lambda:
button_click("+"))
button_subtract = tk.Button(window, text="-", padx=20, pady=10, command=lambda:
button_click("-"))
button_multiply = tk.Button(window, text="*", padx=20, pady=10, command=lambda:
button_click("*"))
button_divide = tk.Button(window, text="/", padx=20, pady=10, command=lambda:
button_click("/"))
button_clear = tk.Button(window, text="C", padx=20, pady=10, command=button_clear)
button_equal = tk.Button(window, text="=", padx=20, pady=10, command=button_equal)
button_sqrt = tk.Button(window, text="√", padx=20, pady=10, command=square_root)
button_square = tk.Button(window, text="x^2", padx=20, pady=10, command=square)

# Position the buttons on the grid


button_1.grid(row=3, column=0)
button_2.grid(row=3, column=1)
button_3.grid(row=3, column=2)
button_4.grid(row=2, column=0)
button_5.grid(row=2, column=1)
button_6.grid(row=2, column=2)
button_7.grid(row=1, column=0)
button_8.grid(row=1, column=1)
button_9.grid(row=1, column=2)
button_0.grid(row=4, column=0)

button_add.grid(row=1, column=3)
button_subtract.grid(row=2, column=3)
button_multiply.grid(row=3, column=3)
button_divide.grid(row=4, column=3)
button_clear.grid(row=4, column=1)
button_equal.grid(row=4, column=2)
button_sqrt.grid(row=1, column=4)
button_square.grid(row=2, column=4)

# Start the main loop


window.mainloop()

You might also like