Skip to content

Instantly share code, notes, and snippets.

@johannvonissou
Created February 7, 2020 08:51
Show Gist options
  • Save johannvonissou/c09c9d4581e83ed4c871a9c236828f1d to your computer and use it in GitHub Desktop.
Save johannvonissou/c09c9d4581e83ed4c871a9c236828f1d to your computer and use it in GitHub Desktop.
from tkinter.messagebox import *
from tkinter import *
import math
def valid(win, n):
"""
Affiche une boite de dialogue informative communiquant le résulat
win: fenetre tkinter
n: nombre d'itérations
Retourne None
"""
vn = int(n)
result = archimede(vn)
ncot = 4*2**vn
showinfo("Résultat", "PI = {r} ; avec un ncot = {ncot}".format(r=result, ncot=ncot))
def show():
"""
Créé la boite de dialogue et l'affiche
Retourne None
"""
win = Tk()
label = Label(win, text="Nombre d'itérations (sachant que nb. cotés = 4*2^n)")
label.pack()
value = StringVar()
entry = Entry(win, textvariable=value, width=30)
entry.pack()
button = Button(win, text="Valider", command=lambda: valid(win, value.get()))
button.pack()
win.mainloop()
def archimede(n):
"""
Calcule PI avec la méthode d'Archimède
n: nombre d'itérations, polygone de 4*2**n côté inscrit dans un cercle de 1 de rayon
Retourne la valeur approchée de PI (float).
"""
poly_cot = 2.0
ncot = 4
for i in range(n):
poly_cot = 2 - 2 * math.sqrt(1 - poly_cot / 4)
ncot *= 2
print("ncot=", ncot)
return ncot * math.sqrt(poly_cot) / 2
show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment