Widget Tkinter qui génère un galvanomètre dont la valeur affichée est modifiable par la commande SetValue()
Source / Exemple :
# -*- coding: cp1252 -*-
# Galvanomètre développé pour la librairie Tkinter
# Ecrit et immaginé par Amaury
# Yvan : ajouté la version non linéaire
from Tkinter import *
from math import *
import time
import random
class Galva(Canvas):
def __init__(self,parent):
global valeur,root,aiguille,txt
root=parent
valeur=0.
b=0
a=0
Canvas.__init__(self)
#Dessin du Galva
self.create_rectangle(0,0,200,200,fill="white",outline="white")
self.create_rectangle(10,10,190,150,width=2,fill="white")
self.create_rectangle(10,120,190,150,width=2,fill="grey")
self.create_oval(25,25,175,175,width=2)
self.create_rectangle(12,60,188,118,fill="white",outline="white")
self.create_rectangle(12,121,188,148,fill="grey",outline="grey")
self.create_rectangle(10,151,190,200,fill="white",outline="white")
self.create_oval(90,125,110,145, width=2,fill="white")
self.create_line(108,127,92,143,width=5)
self.create_oval(95,100,105,110,fill="red",outline="red")
a=(3-(0/50.))*(pi/4)
x=100+90*cos(a)
y=105-90*sin(a)
aiguille=self.create_line(100,105,x,y,width=1,fill="red")
txt=self.create_text(12,160, text="0")
parent.update()
def SetValue(self,consigne):
global valeur,root,aiguille,txt
parent=root
while (int(valeur*100+.5)!=int(consigne*100+.5)):
if (valeur<consigne):
valeur=valeur+(float(consigne-valeur)/50)
float (valeur)
a=(3-(valeur/50.))*(pi/4)
x=100+90*cos(a)
y=105-90*sin(a)
self.delete(aiguille)
self.delete(txt)
aiguille=self.create_line(100,105,x,y,width=1,fill="red")
txt=self.create_text(12,160, text=int(valeur) )
parent.update()
time.sleep(0.002) #Définie l'inertie de l'aiguille (Timer)
elif(valeur>consigne):
valeur=valeur+(float(consigne-valeur)/50)
float (valeur)
a=(3-(valeur/50.))*(pi/4)
x=100+90*cos(a)
y=105-90*sin(a)
self.delete(aiguille)
self.delete(txt)
aiguille=self.create_line(100,105,x,y,width=1,fill="red")
txt=self.create_text(12,160, text=int(valeur) )
parent.update()
time.sleep(0.002) #Définie l'inertie de l'aiguille (Timer)
else:
float (valeur)
a=(3-(valeur/50.))*(pi/4)
x=100+90*cos(a)
y=105-90*sin(a)
self.delete(aiguille)
self.delete(txt)
aiguille=self.create_line(100,105,x,y,width=1,fill="red")
txt=can.create_text(12,160, text=int(valeur) )
parent.update()
time.sleep(0.002) #Définie l'inertie de l'aiguille (Timer)
def SetMinValue(self,valeur):
global valmin
try:
self.delete(valmin)
except:
pass
valmin=self.create_text(30,30,text=str(valeur))
def SetMaxValue(self,valeur):
global valmax
try:
self.delete(valmax)
except:
pass
valmax=self.create_text(170,30,text=str(valeur))
def SetUnit(self,unite):
global unit
try:
self.delete(unit)
except:
pass
unit=self.create_text(100,50,text=str(unite))
def AddScale(self,nombre):
b=0
self.delete(ALL)
self.create_rectangle(0,0,200,200,fill="white",outline="white")
self.create_rectangle(10,10,190,150,width=2,fill="white")
self.create_rectangle(10,120,190,150,width=2,fill="grey")
self.create_oval(25,25,175,175,width=2)
self.create_rectangle(12,60,188,118,fill="white",outline="white")
self.create_rectangle(12,121,188,148,fill="grey",outline="grey")
self.create_rectangle(10,151,190,200,fill="white",outline="white")
self.create_oval(90,125,110,145, width=2,fill="white")
self.create_line(108,127,92,143,width=5)
self.create_oval(95,100,105,110,fill="red",outline="red")
a=(3-(0/50.))*(pi/4)
x=100+90*cos(a)
y=105-90*sin(a)
aiguille=self.create_line(100,105,x,y,width=1,fill="red")
txt=self.create_text(12,160, text="0")
while (b<=100):
a=(3-(b/50.))*(pi/4)
x1=100+90*cos(a)
y1=105-90*sin(a)
x2=100+85*cos(a)
y2=105-85*sin(a)
self.create_line(x1,y1,x2,y2)
b=b+(100/(nombre-1))
def RemScale(self):
self.delete(ALL)
self.create_rectangle(0,0,200,200,fill="white",outline="white")
self.create_rectangle(10,10,190,150,width=2,fill="white")
self.create_rectangle(10,120,190,150,width=2,fill="grey")
self.create_oval(25,25,175,175,width=2)
self.create_rectangle(12,60,188,118,fill="white",outline="white")
self.create_rectangle(12,121,188,148,fill="grey",outline="grey")
self.create_rectangle(10,151,190,200,fill="white",outline="white")
self.create_oval(90,125,110,145, width=2,fill="white")
self.create_line(108,127,92,143,width=5)
self.create_oval(95,100,105,110,fill="red",outline="red")
a=(3-(0/50.))*(pi/4)
x=100+90*cos(a)
y=105-90*sin(a)
aiguille=self.create_line(100,105,x,y,width=1,fill="red")
txt=self.create_text(12,160, text="0")
Conclusion :
Ce programme peut être utilisé à l'intérieur d'une autre application graphique.
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.