c'est le sujet 8 du tp tkinter.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

103 lines
4.1 KiB

import tkinter as tk
# import matplotlib.pyplot as plt
# from matplotlib.pyplot import figure
import numpy as np
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
fen = tk.Tk()
home = tk.Frame()
fen.title("Calculateur d'IMC")
#taille de la fenetre
fen.geometry("950x750")
canvas=tk.Canvas(fen, width=600, height=500, bg="white") #canvas de droite
blanc1 = tk.PhotoImage(file = 'blanc1.png')
image_container1= canvas.create_image(0, 0,anchor=tk.NW,image = blanc1)
canvas.grid(column=1,row=0) #son placement
canvas2=tk.Canvas(fen, width=225, height=500, bg="white") #canvas de gauche
blanc = tk.PhotoImage(file = 'blanc.png')
image_container2= canvas2.create_image(40, 10,anchor=tk.NW, image = blanc)
canvas2.grid(column=0, row=0) #son placement
lbl1=tk.Label(fen, text="votre poids (en kg)", font='Arial') #label poids
lbl1.grid(column=0, columnspan=2, padx=10, pady=10, row=1)
entree_kg = tk.Entry(fen) #entry poids
entree_kg.grid(column=0, columnspan=2,ipadx=50, padx=10, pady=10, row=2)
lbl2=tk.Label(fen, text="votre taille (en cm)", font='Arial') #label taille
lbl2.grid(column=0, columnspan=2, padx=10, pady=10, row=3)
entree_cm = tk.Entry(fen) #entry taille
entree_cm.grid(column=0, columnspan=2,ipadx=50, padx=10, pady=10, row=4)
Label1=tk.Label(fen, text=" ", font=('Arial'))
Label1.grid(column=1, row=1, sticky="e", padx=50, pady=10)
#photos
image_norm = tk.PhotoImage(file = 'normal.png')
image_obes_s = tk.PhotoImage(file = 'obesite severe.png')
image_obes = tk.PhotoImage(file = 'obesite.png')
image_sousp = tk.PhotoImage(file = 'sous poids.png')
image_surpoids = tk.PhotoImage(file = 'surpoids.png')
#fonction qui affiche image imc+petite phrase
def afficher_imc():
taille = float(entree_cm.get())/100 #convertir cm en m
poids= float(entree_kg.get())
imc =float(poids/taille**2) #calcul imc
if imc <=18.4:
Label1.config(text="Vous êtes en sous-poids")
canvas2.itemconfig(image_container2,image=image_sousp)
if imc >=18.5 and imc<=24.9:
Label1.config(text="Votre poids est normal")
canvas2.itemconfig(image_container2,image=image_norm)
if imc >=25.0 and imc<=29.9:
Label1.config(text="Vous êtes en sur poids")
canvas2.itemconfig(image_container2,image=image_surpoids)
if imc >=30.0 and imc<=34.9:
Label1.config(text="Vous etes en obesité")
canvas2.itemconfig(image_container2,image=image_obes)
if imc >=35.0:
Label1.config(text="Vous etes en obesité sévere")
canvas2.itemconfig(image_container2,image=image_obes_s)
lbl=tk.Label(fen, text="Votre imc est de :", font='Arial')
lbl.grid(column=0,row=1)
lbl=tk.Label(fen, text=imc, font='Arial') #afficher le resultat calcul imc
lbl.grid(column=0, row=2)
# graphique
fig = Figure(figsize=(7.2, 5.2), dpi=96)
ax = fig.add_subplot(111)
ax.axis([100, 220, 0, 180]) #limite du graphe
x = np.linspace(100, 220) # les tailles extrèmes en cm
# cm//m
ax.fill_between(x, 0, 18.4 * (x/100)**2, label="sous poids",color="LightBlue") # sous poids si 0 <= y <= 18.4 * x²
ax.fill_between(x, 18.5 * (x/100)**2, 24.9 * (x/100)**2, label="poids idéal",color="green") # idéal si 18.5 * x² <= y <= 24.9 * x²
ax.fill_between(x, 25 * (x/100)**2, 29.9 * (x/100)**2, label="surpoids",color="yellow") # surpoids si 25 * x² <= y <= 29.9 * x²
ax.fill_between(x, 30 * (x/100)**2, 34.9 * (x/100)**2, label="obesité",color="orange") # obésité si 30 * x² <= y <= 34.9 * x²
ax.fill_between(x,180, 35 * (x/100)**2, 40 * (x/100)**2, label="obesité sévère",color="red") # obesité sevère si 35 * x² <= y
ax.scatter(taille*100, poids, color='black')# point a placer sur le graphique
graph = FigureCanvasTkAgg(fig, master=fen)
canvas = graph.get_tk_widget()
canvas.grid(row=0, column=1)
bouton=tk.Button(fen, text="Afficher l'IMC", command=afficher_imc)
bouton.grid(column=0, columnspan=2,ipadx=50, padx=10, pady=10, row=6)
fen.mainloop()