# Fraction Calculator GUI

Page 1 of 1

## 4 Replies - 517 Views - Last Post: 21 July 2014 - 10:20 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=350559&amp;s=f41edb9916ef5b331c2f9fe1bf4de676&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Bungo Mungo

Reputation: 1
• 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)

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

• I'm not here to twist your niblets

Reputation: 4276
• Posts: 15,682
• 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.

### #3 Bungo Mungo

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

## Re: Fraction Calculator GUI

Posted 21 July 2014 - 06:21 AM

andrewsw, 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?

### #4 andrewsw

• I'm not here to twist your niblets

Reputation: 4276
• Posts: 15,682
• 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

### #5 fatihmert

Reputation: 0
• Posts: 82
• Joined: 04-March 12

## Re: Fraction Calculator GUI

Posted 21 July 2014 - 10:20 AM

And Python all GUI librarys here official site.