1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| from tkinter import *
# Position de l'interrupteur ouvert et fermé sur le circuit électrique:
def interrupteur():
global c
if c == False:
intO = can.create_line(153, 30, 246, 60, fill= "white")
intO = can.create_line(153, 60, 246, 60, fill= "black")
c = True
else:
intF = can.create_line(153, 60, 246, 60, fill= "white")
intO = can.create_line(153, 30, 246, 60, fill= "black")
c = False
def evaluer(event):
global c
if c == True:
U = eval(entU.get())
R = eval(entR.get())
resI.configure(text= "L'intensité est de: "+str(U/R)+" A")
I = eval(str(U/R))
resU.configure(text= "U = "+str(R*I)+" V")
resR.configure(text= "R = "+str(U/I)+" Ω")
# Si l'interrupteur est ouvert affichage du Label résultat par défaut:
else:
resU.configure(text= "U = 0 V")
resR.configure(text= "R = 0 Ω")
resI.configure(text= "L'intensité est de: 0 A")
c = False
# Création de la fenêtre:
fen = Tk()
fen.title("Circuit Electrique Simple")
# Création du canevas:
can = Canvas(fen, bg= "white", height= 260, width= 400)
can.grid(row= 0, column= 0, sticky= W, padx= 5)
# Création du circuit électrique dans le canevas:
can.create_rectangle(60, 60, 340, 240, outline= "black")
# Création du générateur dans le canevas:
can.create_oval(30, 120, 90, 180, outline= "black", fill= "light green")
can.create_text(60, 150, text= "G")
# Création de la résistance dans le canevas:
can.create_rectangle(330, 180, 350, 120, outline= "black", fill= "light blue")
can.create_text(340, 150, text= "R")
# Création de l'interrupteur dans le canevas:
can.create_oval(148, 58, 152, 62, outline= "black", fill= "black")
can.create_oval(248, 58, 252, 62, outline= "black", fill= "black")
intF = can.create_line(153, 60, 246, 60, fill= "white")
intO = can.create_line(153, 30, 246, 60, fill= "black")
# Création de Labels Intitulés:
# Tension:
Label(fen, text="U =").grid(row= 1, column= 0, sticky= W)
# Résistance:
Label(fen, text="R =").grid(row= 2, column= 0, sticky= W)
# Création des champs d'entrées:
# Tension:
entU = Entry(fen)
entU.grid(row= 1, column= 0, sticky= W, padx= 30)
entU.bind("<Return>", evaluer)
# Résistance:
entR = Entry(fen)
entR.grid(row= 2, column= 0, sticky= W, padx= 30)
entR.bind("<Return>", evaluer)
# Création des Labels résultats:
# Tension:
resU = Label(fen, text= "U = 0 V")
resU.grid(row= 1, column= 0, sticky = W, padx= 180)
# Résistance:
resR = Label(fen, text= "R = 0 Ω")
resR.grid(row= 2, column= 0, sticky = W, padx= 180)
# Intensité:
resI = Label(fen, text= "L'intensité est de: 0 A")
resI.grid(row= 3, column= 0, sticky= W)
# Création du bouton marche et arrêt:
bouI = Button(fen, text= " On / Off", command= interrupteur)
bouI.grid(row= 0, column= 0, sticky= W, padx= 160)
fen.mainloop() |
Partager