Partage
  • Partager sur Facebook
  • Partager sur Twitter

Problème d'actualisation Tkinter

    24 février 2016 à 23:01:52

    Bonjour à tous,

    Je débute avec Tkinter. J'ai fait un script assez bête dans lequel des carrés tombent les uns à la suite des autres dans une fenêtre, et que l'on peut positionner. Mon problème est que la vitesse des objets augmente sans que je ne comprenne pourquoi...

    Auriez-vous une idée de mon erreur?

    Voici mon code :

    from Tkinter import *
    import math, random, time	
    
    def Deplacement() :
    	global x1, x2, y1, y2, HAUTEUR, carre, VITESSE
    	actualisation = VITESSE
    	if not can_move() :
    		Rand_coords()
    		carre=Canevas.create_rectangle(x1,y1,x2,y2,fill='yellow')
    		fenetre.after(actualisation,Deplacement)
    	if can_move() :
    		y1 += 7 ; y2 += 7
    		Canevas.coords(carre,x1,y1,x2,y2)
    		Gere_fleches()
    		fenetre.after(actualisation,Deplacement)
    		#
    	
    def Rand_coords() :
    	global x1, x2, y1, y2, HAUTEUR
    	# Taille des carres 70x70
    	x1 = random.randint(1,9)*70 ; x2 = x1+70
    	y1 = 0 ; y2 = 70
    
    def can_move() : 
    	global y2, HAUTEUR
    	if y2 >= HAUTEUR :
    		return FALSE
    	if len(Canevas.find_overlapping(x1+1, y1, x2-1, y2)) != 1 :
    		return FALSE
    	else : 
    		return TRUE
    
    def Gere_fleches() :
    	global x1, x2, y1, y2
    	fenetre.bind("<Left>", Dplt_gauche)
    	fenetre.bind("<Right>", Dplt_droit)
    	fenetre.bind("<Down>", Dplt_bas)
    
    
    def Dplt_gauche(event) :
    	global x1, x2, y1, y2
    	print len(Canevas.find_overlapping(x1-70, y1, x2-70, y2))# != 1 :
    		#print 'yes'
    	if x1 - 70 >= 0 and len(Canevas.find_overlapping(x1-75, y1, x2, y2)) == 1 :
    		x1 -= 70 ; x2 -= 70
    		Canevas.coords(carre,x1,y1,x2,y2)
    
    def Dplt_droit(event) :
    	global x1, x2, y1, y2, LARGEUR
    	if x2 + 70 <= LARGEUR and len(Canevas.find_overlapping(x1, y1, x2+75, y2)) == 1 :
    		x1 += 70 ; x2 += 70
    		Canevas.coords(carre,x1,y1,x2,y2)
    
    def Dplt_bas(event) :
    	global x1, x2, y1, y2, HAUTEUR
    	if y2 + 0.7 <= HAUTEUR and  len(Canevas.find_overlapping(x1, y1, x2, y2-75)) == 1 :
    		y1+=7 ; y2 += 7
    		Canevas.coords(carre,x1,y1,x2,y2)
    
    
    HAUTEUR = 700
    LARGEUR = 700
    VITESSE = 40
    
    fenetre= Tk()
    
    # Creation du Canevas
    Canevas = Canvas(fenetre,height=HAUTEUR,width=LARGEUR,bg='gray')
    Canevas.pack(padx=5,pady=5)
    
    Rand_coords()
    carre=Canevas.create_rectangle(x1,y1,x2,y2,fill='yellow')
    Deplacement()
    
    fenetre.mainloop()


    Merci d'avance!!

    • Partager sur Facebook
    • Partager sur Twitter

    Problème d'actualisation Tkinter

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
    • Editeur
    • Markdown