4 Replies - 437 Views - Last Post: 21 July 2014 - 10:20 AM Rate Topic: -----

#1 Bungo Mungo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 23-June 14

Fraction Calculator GUI

Posted 17 July 2014 - 11:31 AM

Hello,

I recently wrote a simple fraction calculator that has add, subtract, multiply, divide, and reduce methods. Everything works fine, but I want to add a GUI to it. I have messed around with tkinter a little bit, but I'm not sure how I can use it to create a graphical calculator application. Here is the code I have written so far:

from fractions import gcd


def mult(n1, d1, n2, d2):
    
    num = n1*n2
    den = d1*d2
    
    return (num, den)

def div(n1, d1, n2, d2):
    
    num = n1*d2
    den = d1*n2
    
    return (num,den)

def add(n1, d1, n2, d2):
    
    if d1 == d2:
        return (n1+n2, d1)
    else:
        n1, n2 = n1*d2, n2*d1
        d = d1*d2
        return (n1+n2, d)

def sub(n1, d1, n2, d2):
    
    if d1 == d2:
        return (n1-n2, d1)
    else:
        n1, n2 = n1*d2, n2*d1
        d = d1*d2
        return (n1-n2, d)
        

def reduce(num, den):
    
    divisor = gcd(num, den)
    if divisor <= 1:
        return None
    else:
        return (num/divisor, den/divisor)
    
def showSol(sol):
    
    redSol = reduce(sol[0], sol[1])
    if redSol == None:
        print("%d/%d" % (sol[0], sol[1]))
    else:
        print("%d/%d" % (redSol[0], redSol[1]))
    


def main():
    
    op = input("Operation (+, -, *, /) > ")
    n1, d1 = eval(input("Enter first fraction (num, den) > "))
    n2, d2 = eval(input("Enter second fraction (num, den) > "))
    if op == "*":
        sol = mult(n1, d1, n2, d2)
        showSol(sol)
    if op == "/":
        sol = div(n1, d1, n2, d2)
        showSol(sol)
    if op == "+":
        sol = add(n1, d1, n2, d2)
        showSol(sol)
    if op == "-":
        sol = sub(n1, d1, n2, d2)
        showSol(sol)
    


if __name__ == '__main__':
    main()



What might be a good place to start or what might be some useful tools to use? Any advice is appreciated.
Also, if you have any ideas on how to refactor my code or make it more efficient those are welcome as well.

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Fraction Calculator GUI

#2 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3822
  • View blog
  • Posts: 13,543
  • Joined: 12-December 12

Re: Fraction Calculator GUI

Posted 17 July 2014 - 11:43 AM

I recommend that you concentrate on learning the basics of tkinter first, before making any attempt to build the GUI for your calculator. GUI programming is event-driven and requires a different thought process compared to a simple (linear) console application.
Was This Post Helpful? 1
  • +
  • -

#3 Bungo Mungo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 23-June 14

Re: Fraction Calculator GUI

Posted 21 July 2014 - 06:21 AM

View Postandrewsw, on 17 July 2014 - 11:43 AM, said:

I recommend that you concentrate on learning the basics of tkinter first, before making any attempt to build the GUI for your calculator. GUI programming is event-driven and requires a different thought process compared to a simple (linear) console application.


Ok cool, are there any good websites/books/tutorials/projects that you might be able to suggest?
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3822
  • View blog
  • Posts: 13,543
  • Joined: 12-December 12

Re: Fraction Calculator GUI

Posted 21 July 2014 - 06:27 AM

I don't know of one to recommend myself, but the TkInter Wiki has a number of links, might be a place to start.

Maybe this one:

http://www.tkdocs.com/tutorial/

(there is a drop-down on the right to filter it to just Python code)

This post has been edited by andrewsw: 21 July 2014 - 06:31 AM

Was This Post Helpful? 1
  • +
  • -

#5 fatihmert  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 04-March 12

Re: Fraction Calculator GUI

Posted 21 July 2014 - 10:20 AM

And Python all GUI librarys here official site.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1