Boucle qui se répète sur tkinter
Lilaue
-
Phil_1857 Messages postés 1872 Date d'inscription Statut Membre Dernière intervention -
Phil_1857 Messages postés 1872 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 :
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
A voir également:
- Boucle qui se répète sur tkinter
- Pc qui s'allume et s'éteint en boucle - Forum Virus
- Tv orange chargement en boucle ✓ - Forum TV & Vidéo
- Xiaomi s'éteint tout seul et se rallume en boucle - Forum Xiaomi
- Boucle excel sans macro - Forum Excel
- Mon PC s'allume puis s'éteint tout seul. Que faire ? ✓ - Forum Matériel & Système
7 réponses
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):
Ensuite, bien évidemment, tu appelles ta fonction pour l'exécuter une 1ere fois:
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
et n'oublie pas de corriger
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()
yg_be
Messages postés
23541
Date d'inscription
Statut
Contributeur
Dernière intervention
Ambassadeur
1 584
bonjour,
ton programme fonctionne bien?
merci de tenir comte de ceci: https://codes-sources.commentcamarche.net/faq/11288-les-balises-de-code
ton programme fonctionne bien?
merci de tenir comte de ceci: https://codes-sources.commentcamarche.net/faq/11288-les-balises-de-code
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
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
plutot que
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
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
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question