My Programs
Math Calculator
from tkinter import *
from tkinter import messagebox
def computeLCM(x, y):
greater = 0
smaller = 0
i = 1
if x < y:
greater = y
smaller = x
else:
greater = x
smaller = y
while True:
if (greater*i) % smaller == 0:
break
else:
i += 1
return greater * i
def computeProduct(a, b):
c = a * b
return c
def computeFactors(n):
z = []
a = n // 2
for i in range(1, a + 1):
if n % i == 0:
z.append(i)
z.append(n)
return (z)
def computeHCF(a, b):
#Create lists with factors of the given parameters and sort them.
f1 = computeFactors(a)
f2 = computeFactors(b)
f1.sort()
f2.sort()
#Create two parameters & default hcf value.
p1 = 0
p2 = 0
hcf = 1
#Create a while loop to find hcf using conditional statements.
while (p1 < len(f1)) & (p2 < len(f2)):
if f1[p1] == f2[p2]:
hcf = f1[p1]
p1 += 1
p2 += 1
elif f1[p1] > f2[p2]:
p2 += 1
else:
p1 += 1
return hcf
def computeHCF_method2(n1, n2):
#Set default values for remainder bigger no. and smaller no..
smaller = 0
bigger = 0
rem = 1
#Conditional statements to find smaller and bigger no..
if n1 < n2:
smaller = n1
bigger = n2
else:
smaller = n2
bigger = n1
#Create a while loop to return the result using modulus while rem > 0.
while rem > 0:
try:
rem = bigger % smaller
except ZeroDivisionError:
break
bigger = smaller
smaller = rem
#Returns the result.
return bigger
#create a window
window = Tk()
window.geometry("1000x800")
window.title("Math Library")
window.configure(bg="blue")
#Create frames
frame1 = Frame(window, height=118, width=500, relief=RIDGE, bd=3, bg="light blue")
frame1.place(x=5,y=5)
frame2 = Frame(window, height=130, width=500, relief=RIDGE, bd=3, bg="light blue")
frame2.place(x=5, y=125)
frame3 = Frame(window, height=130, width=500, relief=RIDGE, bd=3, bg="light blue")
frame3.place(x=5, y=260)
frame4 = Frame(window, height=130, width=500, relief=RIDGE, bd=3, bg="light blue")
frame4.place(x=5, y=395)
#Create labels
lb = Label(frame1, text="COMPUTE FACTORS", bg="light green")
lb.place(x=10, y=10)
lbl = Label(frame1, text="Enter a number", bg="light green")
lbl.place(x=10, y=50)
#Create an Entry box
entry = Entry(frame1, bg="red", fg="gold")
entry.place(x=200,y=50)
#define a function to show a message box with the value in the Entry widget
def get_value():
value = int(entry.get())
factors = computeFactors(value)
messagebox.showinfo("Value", ','.join(str(e) for e in factors))
#Create a button
button = Button(frame1, text="Calculate factors",command=get_value, fg="red", bg="green")
button.place(x=10, y=80)
#Create labels
lbl1 = Label(frame2, text="COMPUTE PRODUCT", bg="light green")
lbl1.place(x=10, y=10)
lbl2 = Label(frame2, text="Enter the first number", bg="light green")
lbl2.place(x=10, y=50)
lbl3 = Label(frame2, text="Enter the second number", bg="light green")
lbl3.place(x=10, y=90)
#Create Entry boxes
entry1 = Entry(frame2, bg="blue", fg="silver")
entry1.place(x=200, y=50)
entry2 = Entry(frame2, bg="blue", fg="silver")
entry2.place(x=200, y=90)
#define a function to calculate product using the simplemath library
def get_product():
val1 = int(entry1.get())
val2 = int(entry2.get())
product = computeProduct(val1,val2)
messagebox.showinfo("Product", str(product))
#Create a button with a command
button = Button(frame2, text="Calculate product", command=get_product, fg="red", bg="green")
button.place(x=350, y=5)
#Create labels
lbl1 = Label(frame3, text="COMPUTE HCF", bg="light green")
lbl1.place(x=10, y=10)
lbl2 = Label(frame3, text="Enter the first number", bg="light green")
lbl2.place(x=10, y=50)
lbl3 = Label(frame3, text="Enter the second number", bg="light green")
lbl3.place(x=10, y=90)
#Create Entry boxes
entry3 = Entry(frame3, bg="gold", fg="blue")
entry3.place(x=200, y=50)
entry4 = Entry(frame3, bg="gold", fg="blue")
entry4.place(x=200, y=90)
#Define functions to show a messagebox with the result of computeHCF & computeHCF_method2
def get_HCF():
value1 = int(entry3.get())
value2 = int(entry4.get())
myhcf = computeHCF(value1, value2)
messagebox.showinfo("HCF", str(myhcf))
def get_HCF2():
value1 = int(entry3.get())
value2 = int(entry4.get())
mrhcf = computeHCF_method2(value1, value2)
messagebox.showinfo("HCF", str(mrhcf))
#Create buttons with a command
button = Button(frame3, text="Calculate HCF: 1", fg="red", bg="green", command=get_HCF)
button.place(x=368, y=5)
button1 = Button(frame3, text="Calculate HCF: 2", fg="red", bg="green", command=get_HCF2)
button1.place(x=368, y=45)
#Create labels
lbl = Label(frame4, text="COMPUTE LCM", bg="light green")
lbl.place(x=10, y=10)
lbl1 = Label(frame4, text="Enter the first number", bg="light green")
lbl1.place(x=10, y=50)
lbl2 = Label(frame4, text="Enter the second number", bg="light green")
lbl2.place(x=10, y=90)
#Create Entry boxes
entry5 = Entry(frame4, bg="silver", fg="red")
entry5.place(x=200, y=50)
entry6 = Entry(frame4, bg="silver", fg="red")
entry6.place(x=200, y=90)
# Define a function to show a messagebox with the result of computeLCM
def get_LCM():
vall1 = int(entry5.get())
vall2 = int(entry6.get())
lcm = computeLCM(vall1, vall2)
messagebox.showinfo("LCM", str(lcm))
#Create a button with a command
butt = Button(frame4, text="Calculate LCM", fg="red", bg="green", command=get_LCM)
butt.place(x=368, y=5)
window.mainloop()
#When 'Calculate Factors' is clicked
#Get the number from the text box
#call simplemath.computeFactors with that number
#Print the output on the console
# First size - 600x500