Monday 11 May 2015

Cross-multiplication Calculator (The Rule Of Three)

In this post I will put a code tha makes a 'calculator' that uses cross-multiplication (or like the title says, The Rule Of Three).

Well, (Wikipedia link-here) one can cross-multiply to determine the value of a variable.

In this case it's like:

a * x = b * c
Which ends up (for the 'x' variable) in:

 x = c * b / a

The code is the next one:

 ## Cross-multiplication
from Tkinter import *
import tkMessageBox

def button(root, text, width, row, column, command=None, cursor=None):
    btn = Button(root, text = text, command=command, cursor=cursor, bg = "black", fg = "yellow", width = width)
    btn.grid(row = row, column = column)
    return btn

def entry(root, textvar, width, row, column):
    ent = Entry(root, textvariable = textvar, width = width)
    ent.grid(row = row, column = column)
    return entry

def pr35():
    '''
    a   ->  b
    c   ->  x
    x = c * b / a
    ''
'
    a = a_val.get()
    b = b_val.get()
    c = c_val.get()
    lab_x_val = Label(root, text = " ", bg = "blue", fg = "green", width = entry_width)
    lab_x_val.grid(row = 1, column = 3)

    if a == 0:
        lab_x_val.config(text="a = 0")
    else:
        x = str(c * b / a)
        lab_x_val.config(text = x)

def info():
    a = '''
    All operations follow
    the    next    idea   :

    a   ->  b
    c   ->  x

    x = c * b / a
    '''
    tkMessageBox.showinfo("Information", a)

def h3LP():
    a = '''
    The calculation that is
    being   excuted   is  :

    x = c * b / a

    So the value of   'a'   must
    be different from zero (0).
    '''
    tkMessageBox.showinfo("Help", a)

def cred():
    a = '''
    Jenko Stevens

    May 11th 2015

    Any questions about
    the source code, post it
    in the web page of:

    pytkstuff.blogspot.com

    Or send me and email to:

    mugendmonkey03@gmail.com
    '''
    tkMessageBox.showinfo("Credits", a)

root = Tk()
root.title("Proporcionality")

entry_width     = 12
label_width     = 6
button_width    = 6

a_val = DoubleVar()
b_val = DoubleVar()
c_val = DoubleVar()

# Just messing around
pr35()

bt_a = button(root, "a", label_width, 0, 0)
bt_b = button(root, "b", label_width, 0, 2)
bt_c = button(root, "c", label_width, 1, 0)
bt_x = button(root, "x", label_width, 1, 2)

ent1 = entry(root, a_val, entry_width, 0, 1)
ent2 = entry(root, b_val, entry_width, 0, 3)
ent3 = entry(root, c_val, entry_width, 1, 1)

btnE = button(root, "Exec", button_width, 2, 0, pr35)
btnH = button(root, "??"  , entry_width , 2, 1, h3LP)
btnI = button(root, "Info", button_width, 2, 2, info)
btnC = button(root, "!!"  , entry_width , 2, 3, cred)

root.mainloop()
In this case I've used the module tkMessageBox instead of creating customized pop-up windows. Some of the buttons in the bottom line are the ones that are linked to use message boxes, like:


And some of the buttons doesn't have assigned commands, for now :P.

No comments:

Post a Comment