Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update improved_cALC.py #178

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 67 additions & 88 deletions calculator GUI prog/improved_cALC.py
Original file line number Diff line number Diff line change
@@ -1,107 +1,86 @@
import tkinter as tk

# Initialize the main application window
root = tk.Tk()
root.title("Calculator")
root.geometry('350x460')
root.resizable(0, 0)
root.configure(bg='black')

# Global variables for operands and operator
fnum = sec_num = operator = None
root.title("Calculator")

# Function to set the operator and clear the display
def getop(op):
global fnum, operator
fnum = int(result_lable['text'])
fnum = int(result_label['text'])
operator = op
clr()

# Function to append numbers to the display
def print_text(s):
current = result_lable['text']
current = result_label['text']
new = current + str(s)
result_lable.config(text = new)
result_label.config(text=new)

# Function to clear the display
def clr():
result_lable.config(text='')
result_label.config(text='')

# Function to calculate and display the result
def result():
global fnum, operator, sec_num

sec_num = int(result_lable['text'])

if(operator == '+'):
result_lable.config(text=str(fnum+sec_num))
elif(operator == '-'):
result_lable.config(text=str(fnum-sec_num))
elif(operator == '*'):
result_lable.config(text=str(fnum*sec_num))
else:
if(sec_num==0):
result_lable.config(text='Error')
sec_num = int(result_label['text'])

if operator == '+':
result_label.config(text=str(fnum + sec_num))
elif operator == '-':
result_label.config(text=str(fnum - sec_num))
elif operator == '*':
result_label.config(text=str(fnum * sec_num))
elif operator == '/':
if sec_num == 0:
result_label.config(text='Error')
else:
result_lable.config(text=str(round(fnum/sec_num)))

root.geometry('350x460')
root.resizable(0,0)
root.configure(bg='black')

result_lable = tk.Label(root, text='', bg='black', fg='white')
result_lable.grid(row=0,column=0,pady=(40,20),columnspan=6,sticky='w')
result_lable.config(font=('verdana', 30, 'bold'))



btn7 = tk.Button(root, text='7', bg='green', fg='white',width = 7, height =3,command=lambda: print_text(7))
btn8 = tk.Button(root, text='8', bg='green', fg='white',width = 7, height =3,command=lambda:print_text(8))
btn9 = tk.Button(root, text='9', bg='green', fg='white',width = 7, height =3,command=lambda:print_text(9))


btn4 = tk.Button(root, text='4', bg='green', fg='white',width = 7, height =3,command=lambda:print_text(4))
btn5 = tk.Button(root, text='5', bg='green', fg='white',width = 7, height =3,command=lambda:print_text(5))
btn6 = tk.Button(root, text='6', bg='green', fg='white',width = 7, height =3,command=lambda:print_text(6))

btn1 = tk.Button(root, text='1', bg='green', fg='white',width = 7, height =3,command=lambda:print_text(1))
btn2 = tk.Button(root, text='2', bg='green', fg='white',width = 7, height =3,command=lambda:print_text(2))
btn3 = tk.Button(root, text='3', bg='green', fg='white',width = 7, height =3,command=lambda:print_text(3))
btn0 = tk.Button(root, text='0', bg='green', fg='white',width = 7, height =3,command=lambda:print_text(0))

btn_add = tk.Button(root, text='+', fg = 'white', bg='green',width = 7, height=3,command= lambda: getop('+'))
btn_sub = tk.Button(root, text='-', fg = 'white', bg='green',width = 7, height=3,command= lambda: getop('-'))
btn_mult = tk.Button(root, text='*', fg = 'white', bg='green',width = 7, height=3,command= lambda: getop('*'))
btn_div = tk.Button(root, text='/', fg = 'white', bg='green',width = 7, height=3,command= lambda: getop('/'))
btn_eq = tk.Button(root, text='=', fg = 'white', bg='green',width = 7, height=3,command=lambda: result())
btn_clr = tk.Button(root, text='C', fg = 'white', bg='green',width = 7, height=3,command= clr)


btn0.grid(row=7,column=1)
btn0.config(font=('verdana,14,bold'))
btn7.grid(row=4,column=0)
btn7.config(font=('verdana,14,bold'))
btn8.grid(row=4,column=1)
btn8.config(font=('verdana,14,bold'))
btn9.grid(row=4,column=2)
btn9.config(font=('verdana,14,bold'))
btn4.grid(row=5,column=0)
btn4.config(font=('verdana,14,bold'))
btn5.grid(row=5,column=1)
btn5.config(font=('verdana,14,bold'))
btn6.grid(row=5,column=2)
btn6.config(font=('verdana,14,bold'))

btn1.grid(row=6,column=0)
btn1.config(font=('verdana,14,bold'))
btn2.grid(row=6,column=1)
btn2.config(font=('verdana,14,bold'))
btn3.grid(row=6,column=2)
btn3.config(font=('verdana,14,bold'))

btn_add.grid(row=4,column=3)
btn_add.config(font=('verdana,14,bold'))
btn_sub.grid(row=5,column=3)
btn_sub.config(font=('verdana,14,bold'))
btn_mult.grid(row=6,column=3)
btn_mult.config(font=('verdana,14,bold'))
btn_div.grid(row=7,column=3)
btn_div.config(font=('verdana,14,bold'))

btn_eq.grid(row=7,column=2)
btn_eq.config(font=('verdana,14,bold'))
btn_clr.grid(row=7,column=0)
btn_clr.config(font=('verdana,14,bold'))

root.mainloop()
result_label.config(text=str(round(fnum / sec_num)))

# Create and configure the result label
result_label = tk.Label(root, text='', bg='black', fg='white', font=('verdana', 30, 'bold'))
result_label.grid(row=0, column=0, pady=(40, 20), columnspan=6, sticky='w')

# Create number buttons
buttons = [
('7', 4, 0), ('8', 4, 1), ('9', 4, 2),
('4', 5, 0), ('5', 5, 1), ('6', 5, 2),
('1', 6, 0), ('2', 6, 1), ('3', 6, 2),
('0', 7, 1)
]

for (text, row, col) in buttons:
btn = tk.Button(root, text=text, bg='green', fg='white', width=7, height=3,
command=lambda s=text: print_text(s))
btn.grid(row=row, column=col)
btn.config(font=('verdana', 14, 'bold'))

# Create operator buttons
operators = [
('+', 4, 3), ('-', 5, 3), ('*', 6, 3), ('/', 7, 3),
('=', 7, 2), ('C', 7, 0)
]

for (text, row, col) in operators:
if text == '=':
btn = tk.Button(root, text=text, fg='white', bg='green', width=7, height=3,
command=result)
elif text == 'C':
btn = tk.Button(root, text=text, fg='white', bg='green', width=7, height=3,
command=clr)
else:
btn = tk.Button(root, text=text, fg='white', bg='green', width=7, height=3,
command=lambda s=text: getop(s))

btn.grid(row=row, column=col)
btn.config(font=('verdana', 14, 'bold'))

# Start the main event loop
root.mainloop()