0% found this document useful (0 votes)
5 views3 pages

Calculator

The document describes a scientific calculator application created with Tkinter. It includes frames for input numbers, options, buttons for different mathematical operations, and output results. Functions are defined to add buttons and display results. The application allows for accumulating results and inverse operations.

Uploaded by

bayanmunh11
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)
5 views3 pages

Calculator

The document describes a scientific calculator application created with Tkinter. It includes frames for input numbers, options, buttons for different mathematical operations, and output results. Functions are defined to add buttons and display results. The application allows for accumulating results and inverse operations.

Uploaded by

bayanmunh11
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/ 3

d

from tkinter import Tk, Frame, Label, LEFT, TOP, Button, X, RIGHT, Entry, IntVar,
Checkbutton, Text, WORD, END
from operations import addition, subtraction, division, multiplication, exponent,
rooting, sine, cosine, tangent, logarithem
from os import path

# Make Window
window = Tk()
window.wm_iconbitmap("main.ico")
window.title("MC1323 Calculator V1.1.4")
window.geometry("400x500")

# Add the Title


titleFrame = Frame(border=5, bg='black')

title = Label(
titleFrame,
text="Welcome to MC1323 Calculator",
bg = 'black',
fg = 'white',
font=("Arial", 15)
).pack(side=LEFT)

exitB=Button(
titleFrame,
text='X',
bg='black',
fg='white',
font=("Arial", 15),
command=window.destroy
).pack(side=RIGHT)

titleFrame.pack(fill=X, side=TOP)

# Num Entry In Its Frame


# Frame
numFrame = Frame(border=5)
# Entry for Number 1
num1 = Entry(
width = 15,
master=numFrame
)
# Entry for Number 2
num2 = Entry(
width = 15,
master=numFrame
)
# Labels
Label(
text='First Number:',
master=numFrame
).pack(side=LEFT)
num1.pack(side=LEFT)
num2.pack(side=RIGHT)
Label(
text='Second Number:',
master=numFrame
).pack(side=RIGHT)
# Pack Frame
numFrame.pack(fill=X)

# Options
options = Frame()
Label(options,text='Options:', font=('Ariel', 12, 'bold')).pack(side=TOP)
# Select whether to acumulate values eg. result becomes num1
accumulate = IntVar()
Checkbutton(
options,
border=5,
text="Accumulate", # Label displayed near the checkbox
variable=accumulate, # Link the checkbox to the variable
onvalue=1, # Set value to 1 when checked
offvalue=0, # Set value to 0 when unchecked
).pack(side=LEFT)
# Inverse Function
inverse = IntVar()
Checkbutton(
options,
border=5,
text="Inverse Function", # Label displayed near the checkbox
variable=inverse, # Link the checkbox to the variable
onvalue=1, # Set value to 1 when checked
offvalue=0, # Set value to 0 when unchecked
).pack(side=LEFT)

# Result
result = Text(wrap=WORD)

# Buttons & Their Functions & Their Frame


btnFrame = Frame(borderwidth=10) # Frame 1
btnFrame2 = Frame(borderwidth=10) # Frame 2
btnFrame3 = Frame(borderwidth=10) # Frame 3
# Show Resuls
def setResults(results):
"""Sets the result text widget to display results
ARGUMENTS:
- results: A string which will be displayed on a new line.
"""
result.insert(END, results[0])
result.see(END)
if accumulate.get():
if results[1]:
num1.delete(0, END)
num1.insert(0, results[1])
mixer.music.play()

def addButton(name, func, frame=window):


"""Quickely Creates one of the operation buttons
ARGUMENTS:
- name: A string for the name of the button
- func: the operation function the button will run
- frame: The tkinter frame it will reside in. Defauts to window.
"""
def function():
results = func(num1, num2, inverse)
setResults(results)
Button(master=frame,text=name, command=function).pack(side=LEFT, expand=True)

# Add Buttons
addButton('Add',addition, btnFrame)
addButton('Subtract',subtraction,btnFrame)
addButton('Multiply',multiplication,btnFrame)
addButton('Divide',division,btnFrame)
addButton('Exponent',exponent,btnFrame)
addButton('Root',rooting,btnFrame)
addButton('Sine',sine,btnFrame2)
addButton('Cosine',cosine,btnFrame2)
addButton('Tangent',tangent,btnFrame2)
addButton('Logarithm (SecondNumber for log10())',logarithem,btnFrame2)
addButton('F to C',FtoC,btnFrame3)

# Pakc Frames
btnFrame.pack(side=TOP, fill=X)
btnFrame2.pack(side=TOP, fill=X)
btnFrame3.pack(side=TOP, fill=X)

# Pack and loop.


options.pack(side = TOP)
result.pack(fill=X, side=TOP)
window.mainloop()

You might also like