KEMBAR78
Calculator Combined - Py | PDF | Mathematics | Arithmetic
0% found this document useful (0 votes)
32 views2 pages

Calculator Combined - Py

This document describes code for a basic graphical user interface (GUI) calculator application created with Tkinter. The code defines functions for handling button presses, performing calculations, and clearing the display. It then builds the GUI layout with a grid of buttons and display entry field and runs the main loop.
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)
32 views2 pages

Calculator Combined - Py

This document describes code for a basic graphical user interface (GUI) calculator application created with Tkinter. The code defines functions for handling button presses, performing calculations, and clearing the display. It then builds the GUI layout with a grid of buttons and display entry field and runs the main loop.
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 math

from tkinter import *


from calculator_cli import add, subtract, multiply, divide

# Function to handle button presses


def press(num):
current = expression.get()
expression.set(current + str(num))

def equalpress():
try:
current = expression.get()
result = eval(current)
expression.set(str(result))
except SyntaxError:
expression.set("Invalid Input")
except ZeroDivisionError:
expression.set("Cannot Divide By Zero")
except Exception as e:
expression.set("Error: " + str(e))

def clear():
expression.set("")

def square_root():
current = expression.get()
try:
num = float(current)
if num < 0:
expression.set("Invalid Input")
else:
result = math.sqrt(num)
expression.set(str(result))
except ValueError:
expression.set("Invalid Input")

if __name__ == "__main__":
gui = Tk()
gui.configure(background="light green")
gui.title("Calculator")
gui.geometry("270x150")

expression = StringVar()

expression_field = Entry(gui, textvariable=expression)


expression_field.grid(columnspan=4, ipadx=70)

button1 = Button(gui, text='1', fg='black', bg='red', command=lambda: press(1),


height=1, width=7)
button1.grid(row=2, column=0)

button2 = Button(gui, text='2', fg='black', bg='red', command=lambda: press(2),


height=1, width=7)
button2.grid(row=2, column=1)

button3 = Button(gui, text='3', fg='black', bg='red', command=lambda: press(3),


height=1, width=7)
button3.grid(row=2, column=2)
button4 = Button(gui, text='4', fg='black', bg='red', command=lambda: press(4),
height=1, width=7)
button4.grid(row=3, column=0)

subtract_button = Button(gui, text='-', fg='black', bg='red', command=lambda:


press('-'), height=1, width=7)
subtract_button.grid(row=3, column=1)

multiply_button = Button(gui, text='*', fg='black', bg='red', command=lambda:


press('*'), height=1, width=7)
multiply_button.grid(row=3, column=2)

divide_button = Button(gui, text='/', fg='black', bg='red', command=lambda:


press('/'), height=1, width=7)
divide_button.grid(row=4, column=0)

equals_button = Button(gui, text='=', fg='black', bg='red', command=equalpress,


height=1, width=7)
equals_button.grid(row=4, column=1)

clear_button = Button(gui, text='C', fg='black', bg='red', command=clear,


height=1, width=7)
clear_button.grid(row=4, column=2)

square_root_button = Button(gui, text='√', fg='black', bg='red',


command=square_root, height=1, width=7)
square_root_button.grid(row=5, column=0)

decimal_button = Button(gui, text='.', fg='black', bg='red', command=lambda:


press('.'), height=1, width=7)
decimal_button.grid(row=5, column=1)

open_paren_button = Button(gui, text='(', fg='black', bg='red', command=lambda:


press('('), height=1, width=7)
open_paren_button.grid(row=5, column=2)

close_paren_button = Button(gui, text=')', fg='black', bg='red',


command=lambda: press(')'), height=1, width=7)
close_paren_button.grid(row=5, column=3)

gui.mainloop()

You might also like