Boucle qui se répète sur tkinter

Lilaue -  
Phil_1857 Messages postés 1883 Date d'inscription   Statut Membre Dernière intervention   -
Bonjour,
j’aimerai si possible créer une boucle pour mon programme qui se répéterai toutes les 5 sec sans s’arrêter mais je n’ai rien trouvé.
voici mon programme :

from tkinter import *
import random
import time

      
def random_color():
  return random.randint(0, 0x1000000)
      
def random_outline():
  return random.randint(0, 0x1000000)
    
tk = Tk()
canvas = Canvas(tk, width=500, height=500)
canvas.pack()
      
for x in range(0, 5):
      color = '{:06x}'.format(random_color())
      color2 = '{:06x}'.format(random_color())
      color3 = '{:06x}'.format(random_color())
      outline = '{:06x}'.format(random_outline())
      outline2 = '{:06x}'.format(random_outline())
      outline3 = '{:06x}'.format(random_outline())
      width = random.randint(0, 15)
      width2 = random.randint(0, 15)
      width3 = random.randint(0, 15)
      x1 = random.randint(0, 500)
      y1 = random.randint(0, 500)
      x2 = random.randint(0, 500)
      y2 = random.randint(0, 500)
      x3 = random.randint(0, 500)
      y3 = random.randint(0, 500)
      
      xa = random.randint(0, 500)
      ya = random.randint(0, 500)
      xb = random.randint(0, 500)
      yb = random.randint(0, 500)
      
      x11 = random.randint(0, 500)
      y11 = random.randint(0, 500)
      x22 = random.randint(0, 500)
      y22 = random.randint(0, 500)
      
      my_triangle = canvas.create_polygon(x1, y1, x2, y2, x3, y3,fill =('#'+ color), outline="#"+outline, width=width)
      my_rectangle = canvas.create_rectangle(xa,ya,xb,yb,fill=("#" + color2),outline="#" + outline2,width=width2)
      my_oval = canvas.create_oval(x11,y11,x22,y22,fill=("#" + color3),outline="#"+outline3,width=width3)
      
      
tk.mainloop()


Configuration: iPad / Safari 14.0.1

7 réponses

  1. Phil_1857 Messages postés 1883 Date d'inscription   Statut Membre Dernière intervention   169
     
    En principe, ce forum est fait pour aider mais pas pour donner des solutions toutes faites

    Mais bon ...

    en gros, tout ce qu'il y a dans la boucle for, tu le mets dans une fonction, ça tu sais déjà faire

    puisque tu as déjà défini 2 fonctions dans ton code (il n'y a plus de boucle for):

    def afficher_figures():
          color = '{:06x}'.format(random_color())
          color2 = '{:06x}'.format(random_color())
          color3 = '{:06x}'.format(random_color())
          outline = '{:06x}'.format(random_outline())
          outline2 = '{:06x}'.format(random_outline())
          outline3 = '{:06x}'.format(random_outline())
          width = random.randint(0, 15)
          width2 = random.randint(0, 15)
          width3 = random.randint(0, 15)
          x1 = random.randint(0, 500)
          y1 = random.randint(0, 500)
          x2 = random.randint(0, 500)
          y2 = random.randint(0, 500)
          x3 = random.randint(0, 500)
          y3 = random.randint(0, 500)
          
          xa = random.randint(0, 500)
          ya = random.randint(0, 500)
          xb = random.randint(0, 500)
          yb = random.randint(0, 500)
          
          x11 = random.randint(0, 500)
          y11 = random.randint(0, 500)
          x22 = random.randint(0, 500)
          y22 = random.randint(0, 500)
          
          my_triangle = canvas.create_polygon(x1, y1, x2, y2, x3, y3,fill =('#'+ color), outline="#"+outline, width=width)
          my_rectangle = canvas.create_rectangle(xa,ya,xb,yb,fill=("#" + color2),outline="#" + outline2,width=width2)
          my_oval = canvas.create_oval(x11,y11,x22,y22,fill=("#" + color3),outline="#"+outline3,width=width3)
    


    Ensuite, bien évidemment, tu appelles ta fonction pour l'exécuter une 1ere fois:

    canvas.pack()
    afficher_figures()
    


    Et enfin, pour qu'elle s'appelle elle-même toutes les 5 secondes, tu ajoute en fin de fonction

    la méthode after() : regarde sur internet comment la câbler

    def afficher_figures():
        .................................
        .................................
         my_oval = canvas.create_oval(x11,y11,x22,y22,fill=("#" + color3),outline="#"+outline3,width=width3)
        #ici : méthode after pour rappeler afficher_figures
    


    et n'oublie pas de corriger
     tk.mainloop() 
    1
  2. Lilaue
     
    oups désolée, sinon oui il marche

    
    from tkinter import *
    import random
    import time
    
          
    def random_color():
      return random.randint(0, 0x1000000)
          
    def random_outline():
      return random.randint(0, 0x1000000)
        
    tk = Tk()
    canvas = Canvas(tk, width=500, height=500)
    canvas.pack()
          
    for x in range(0, 5):
          color = '{:06x}'.format(random_color())
          color2 = '{:06x}'.format(random_color())
          color3 = '{:06x}'.format(random_color())
          outline = '{:06x}'.format(random_outline())
          outline2 = '{:06x}'.format(random_outline())
          outline3 = '{:06x}'.format(random_outline())
          width = random.randint(0, 15)
          width2 = random.randint(0, 15)
          width3 = random.randint(0, 15)
          x1 = random.randint(0, 500)
          y1 = random.randint(0, 500)
          x2 = random.randint(0, 500)
          y2 = random.randint(0, 500)
          x3 = random.randint(0, 500)
          y3 = random.randint(0, 500)
          
          xa = random.randint(0, 500)
          ya = random.randint(0, 500)
          xb = random.randint(0, 500)
          yb = random.randint(0, 500)
          
          x11 = random.randint(0, 500)
          y11 = random.randint(0, 500)
          x22 = random.randint(0, 500)
          y22 = random.randint(0, 500)
          
          my_triangle = canvas.create_polygon(x1, y1, x2, y2, x3, y3,fill =('#'+ color), outline="#"+outline, width=width)
          my_rectangle = canvas.create_rectangle(xa,ya,xb,yb,fill=("#" + color2),outline="#" + outline2,width=width2)
          my_oval = canvas.create_oval(x11,y11,x22,y22,fill=("#" + color3),outline="#"+outline3,width=width3)
          
          
    tk.mainloop
    
    
    0
  3. Phil_1857 Messages postés 1883 Date d'inscription   Statut Membre Dernière intervention   169
     
    Bonjour Lilaue,

    Il marche, il marche ....

    dans mon IDE (IDLE) oui, mais pas si on le lance en dehors d'un IDE, en double_cliquant

    sur le fichier par exemple

    Par contre, ca va déjà mieux en écrivant
    tk.mainloop() 

    plutot que
    tk.mainloop


    En fait, tu ne devrais pas faire une boucle de 0 à 5, mais plutôt mettre toute cette partie dans une fonction que

    tu appellerais après la définition de la fenêtre principale

    Et en fin de cette fonction tu appelle la méthode after() pour qu'elle s'appelle elle-même (ta fonction) toutes

    les 5 secondes
    0
  4. Vous n’avez pas trouvé la réponse que vous recherchez ?

    Posez votre question
  5. Lilaue
     
    je ne comprends pas comment faire, est ce que vous pouvez me mettre le code svp
    0
  6. Phil_1857 Messages postés 1883 Date d'inscription   Statut Membre Dernière intervention   169
     
    Bonjour Lilaue,

    En tout cas, chez moi, ca marche super bien

    On voit des figures géométriques colorées aléatoires qui se renouvellent toutes les 5 secondes ...
    0
  7. Phil_1857 Messages postés 1883 Date d'inscription   Statut Membre Dernière intervention   169
     
    Bonjour Lilaue,

    alors, as-tu de la réussite ?
    0