# Fraction Calculator GUI

Page 1 of 1

## 4 Replies - 929 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=67b369df1e8eaa7db8510f2ee0c4af89&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Bungo Mungo

• New D.I.C Head

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)

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

• There's no such thing as the Dog Poo Fairy

Reputation: 5785
• Posts: 22,927
• 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

• New D.I.C Head

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?
Was This Post Helpful? 0

### #4 andrewsw

• There's no such thing as the Dog Poo Fairy

Reputation: 5785
• Posts: 22,927
• 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

• D.I.C Head

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

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}