(GUI) Approximation of PI Value
Here’s the code :
———————————————————————
from graphics import *
import math
def main():
win=GraphWin(“Approximation of PI”, 400,400)
win.setBackground(“yellow”)
# Draw the interface
title = Text(Point(200,75), “APPROXIMATION OF PI”)
title.draw(win)
title.setFill(“blue”)
title.setFace(“courier”)
title.setSize(20)
Text(Point(100,150), “Number of terms”).draw(win)
Text(Point(100,280), ” Approximated PI :”).draw(win)
Text(Point(120,310), ” Math PI – Approximation :”).draw(win)
Text(Point(100,340), ” Percentage of error :”).draw(win)
input = Entry(Point(200,150), 5)
input.setText(“0”)
input.draw(win)
app = Text(Point(250,280), “”)
app.setFill(“blue”)
app.setStyle(“bold”)
app.draw(win)
diff = Text(Point(250,310), “”)
diff.setFill(“blue”)
diff.setStyle(“bold”)
diff.draw(win)
percent = Text(Point(250,340), “”)
percent.setFill(“blue”)
percent.setStyle(“bold”)
percent.draw(win)
button = Text(Point(200,200), “Get The Result”)
button.setStyle(“bold”)
button.setSize(10)
button.setFace(“times roman”)
button.setFill(“red”)
button.draw(win)
ov = Oval(Point(120,180),Point(280,220))
ov.draw(win)
win.getMouse()
button.setText(“Quit”)
x = eval(input.getText())
approx=float(0)
sign = 0
for i in range(1, 2*x, 2):
y=(4./i)*((-1)**sign)
approx=approx+y
sign=sign+1
true_error = math.pi – approx
if(true_error > 0) :
relative_error = true_error / math.pi * 100
else :
relative_error = (-1)*true_error / math.pi * 100
app.setText(“%0.2f” % approx)
diff.setText(“%0.2f” %true_error)
percent.setText(“%0.2f %%” %relative_error)
win.getMouse()
win.close()
main()
———————————————————————————
And here’s the screenshot of the program :
This entry was posted on Friday, March 28th, 2014 at 7:00 pm and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.