IPB

Bienvenue invité ( Connexion | Inscription )

[ En ligne ] · Standard · Linéaire+

> Momo Se Remet à La "programmation", Sponsorised by O'Reilly

momo
post 21/02/2004 1:21
Message #1


lvl1 male rogue St=1 Dex=5 Int=17 Wi=5 Ch=0
****

Groupe : Modérateurs
Messages : 1,730
Inscrit le : 16/05/2002 23:00
Lieu : .gif
Membre no. 12



CODE
from Tkinter import *
def printit(event):
   words=dict([("..--..","?"),("--..--",","),(".-.-.-","."),("----.","9"),("---..","8"),
           ("--...","7"), ("-....","6"), (".....","5"), ("....-","4"), ("...--","3"),
           ("..---","2"), (".----","1"), ("-----","0"), ("--..","z"), ("-.--","y"),
           ("-..-","x"), ("...-","v"), ("--.-","q"), (".---","j"), ("....","h"),
           ("..-.","f"), ("-.-.","c"), ("-...","b"), (".--.","p"), ("..-", "u"), ("...","s"),
           (".-.","r"), ("---","o"), ("-.-","k"), ("--.","g"), ("-..","d"),
           ("-.","n"), ("..","i"), ("--","m"), (".-","a"), (".","e"), ("-","t"),
           (".-..","l"), (".--","w")])
   fichier=unicode(ent.get())
   sfichier=""
   caractere="0"
   counter=0
   while caractere<>"":
       if counter<int(len(fichier)):
           caractere=fichier[counter]
       else:
           caractere=""
       counter=counter+1
       chaine =""
       while ( caractere=="." or caractere=="-"):
           chaine=chaine+caractere
           if counter<int(len(fichier)):
               caractere=fichier[counter]
           else:
               caractere=""
           counter=counter+1
       if words.has_key(chaine):
           schaine=words[chaine]
           chaine=""
           caractere="."
           sfichier=sfichier+unicode(schaine)
       else:
           sfichier=sfichier+unicode(chaine)
           chaine=""

       if (caractere=="." or caractere=="-"):
           caractere="0"
       else:
           sfichier=sfichier+unicode(caractere)
   lab.configure(text=unicode(sfichier))

fen=Tk()
ent=Entry(fen)
lab=Label(fen)
ent.bind("<Return>",printit)
ent.pack()
lab.pack()
fen.mainloop()

Ben ouais, m'ennuyant, je me suis acheté "Apprendre à programmer avec Python" et "Python Précis et concis" (non, pas seulement pour avoir le sadisme de mettre le python en présence de la mangouste wink.gif).
Et comme j'ai toujours ce programme @ la con de decodage de code Morse qui traine ...


--------------------
Natural evolution insists that we are apes; artificial evolution insists that we are machines with an attitude.
Kevin Kelly - Out of control
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
 
Reply to this topicStart new topicStart Poll
Réponse(s)
momo
post 21/02/2004 21:59
Message #2


lvl1 male rogue St=1 Dex=5 Int=17 Wi=5 Ch=0
****

Groupe : Modérateurs
Messages : 1,730
Inscrit le : 16/05/2002 23:00
Lieu : .gif
Membre no. 12



le petit dernier :
CODE

# -*- coding: cp1252 -*-
from Tkinter import *
from math import *
#Données initiales
cvSize=400 #Taille du Canvas
echelle=800000/cvSize #calcul de l'echelle en Km par pixel
eDiam=12756/200 #diametre du premier cercle en pixel
lDiam=3476/200 #diametre du seconde cercle en pixel
m1=5.9742E24 #en kg
m2= 7.349E22 #en kg
distanc=384400 #distance séparant les cercles (en km)
x1=eDiam/2
y1=cvSize/2
x2=x1+ (distanc/echelle)
y2=y1
#Definitions des fonctions de modifications de coordonnees
# cercle 1
def haut():
   global x1,y1
   y1=y1-10
   if y1<0:
       y1=0
   cosmos.coords(earth,x1,y1,x1+eDiam,y1+eDiam)
   l2upd()
def gauche():
   global x1,y1
   x1=x1-10
   if x1<0:
       x1=0
   cosmos.coords(earth,x1,y1,x1+eDiam,y1+eDiam)
   l2upd()
def droite():
   global x1,y1
   x1=x1+10
   if x1>(cvSize-eDiam):
       x1=cvSize-eDiam
   cosmos.coords(earth,x1,y1,x1+eDiam,y1+eDiam)
   l2upd()
def bas():
   global x1,y1
   y1=y1+10
   if y1>(cvSize-eDiam):
       y1=cvSize-eDiam
   cosmos.coords(earth,x1,y1,x1+eDiam,y1+eDiam)
   l2upd()
# cercle 2
def hauta():
   global x2,y2
   y2=y2-10
   if y2<0:
       y2=0
   cosmos.coords(moon,x2,y2,x2+lDiam,y2+lDiam)
   l2upd()
def gauchea():
   global x2,y2
   x2=x2-10
   if x2<0:
       x2=0
   cosmos.coords(moon,x2,y2,x2+lDiam,y2+lDiam)
   l2upd()
def droitea():
   global x2,y2
   x2=x2+10
   if x2>(cvSize-lDiam):
       x2=cvSize-lDiam
   cosmos.coords(moon,x2,y2,x2+lDiam,y2+lDiam)
   l2upd()
def basa():
   global x2,y2
   y2=y2+10
   if y2>(cvSize-lDiam):
       y2=cvSize-lDiam
   cosmos.coords(moon,x2,y2,x2+lDiam,y2+lDiam)
   l2upd()
#Calcul de la distance entre les cercles
def distance():
   x=abs((x2+lDiam/2)-(x1+eDiam/2))
   y=abs((y2+lDiam/2)-(y1+eDiam/2))
   d=sqrt(x**2+y**2)*echelle
   return d
#Calcul de la force gravitationelle entre les cercles
def forceg():
   resultat=6.67E-11*m1*m2/(distance())**2
   return resultat
#Mise à jour du texte de lab2
def l2upd():
   lab2.configure(text="Distance : "+str(distance())+\
   " Kilometres. Force gravitationelle : "+str(forceg())+" Newtons.")
#Fenêtre principale
fen=Tk()
lab1=Label(fen,text="Cercle bleu : "+str(m1)+" kg. Cercle blanc : "+str(m2)+\
          " kg. Echelle : 1 pixel pour "+str(echelle)+" km")
lab1.grid(row=0, column=0)
cosmos=Canvas(fen,bg="black",height=cvSize,width=cvSize)
cosmos.grid(row=1, column=0,rowspan=6)
lab2=Label(fen)
lab2.grid(row=7,column=0)
earth=cosmos.create_oval(x1,y1,x1+eDiam,y1+eDiam,outline="blue",fill="blue")
moon=cosmos.create_oval(x2,y2,x2+lDiam,y2+lDiam,outline="white",fill="white")
haut1=Button(fen,text="Haut",command=haut)
bas1=Button(fen,text="Bas",command=bas)
gauche1=Button(fen,text="Gauche",command=gauche)
droite1=Button(fen,text="Droite",command=droite)
haut2=Button(fen,text="Haut",command=hauta)
bas2=Button(fen,text="Bas",command=basa)
gauche2=Button(fen,text="Gauche",command=gauchea)
droite2=Button(fen,text="Droite",command=droitea)
haut1.grid(row=1,column=2)
bas1.grid(row=3,column=2)
gauche1.grid(row=2,column=1)
droite1.grid(row=2,column=3)
haut2.grid(row=4,column=2)
bas2.grid(row=6,column=2)
gauche2.grid(row=5,column=1)
droite2.grid(row=5,column=3)
l2upd()
fen.mainloop()

m'enfin, vivement que j'avance un peu plus dans ce bouquin, histoire de trouver une solution plus élégante pour les différents boutons ... (sans compter l'immonde hack quand je me suis rendu compte de les cercles n'ont pas le centre comme origine .. tongue.gif)
M'enfin : notez bien, j'ai commenté mon code !!! tongue.gif
(par contre, la précision du prog, c'est pas ça, mais bon, si je voulais des calculs exacts, je ne serai pas parti sur ce genre d'interface à la con ... )



--------------------
Natural evolution insists that we are apes; artificial evolution insists that we are machines with an attitude.
Kevin Kelly - Out of control
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Posts in this topic


Reply to this topicTopic OptionsStart new topic
1 utilisateur(s) sur ce sujet (1 invité(s) et 0 utilisateur(s) anonyme(s))
0 membre(s) :
 

Version bas débit Nous sommes le : : 17/11/2025 8:41