From 7d2cce53939253fd0597f9de3cf3d312e26dc901 Mon Sep 17 00:00:00 2001 From: ROBINET Julien Date: Fri, 27 Mar 2026 14:07:45 +0100 Subject: [PATCH] + fonction spawn_rectangle qui marche --- projet_tkinter_sujet5.py | 78 +++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 45 deletions(-) diff --git a/projet_tkinter_sujet5.py b/projet_tkinter_sujet5.py index 29a1dc9..6d7c334 100644 --- a/projet_tkinter_sujet5.py +++ b/projet_tkinter_sujet5.py @@ -1,46 +1,34 @@ -import tkinter as tk -from random import randint - -indice_couleur = randint(0, 4) -couleur = 0 -if indice_couleur == 0 : - couleur = "red" -if indice_couleur == 1 : - couleur = "green" -if indice_couleur == 2 : - couleur = "blue" -if indice_couleur == 3 : - couleur = "yellow" -if indice_couleur == 4 : - couleur = "grey" - -#taille = randint(10, 30)# -#position_x = randint(0, 400)# -#position_y = randint(0, 300)# -#while position_x+taille > 400 or position_y+taille > 300:# -# taille = randint(10, 31)# -# position_x = randint(0, 400)# -# position_y = randint(0, 300)# - -def spawn_rectangle() : - taille = randint(10, 30) - position_x = randint(0, 400) - position_y = randint(0, 300) - while position_x+taille > 400 or position_y+taille > 300: - taille = randint(10, 31) - position_x = randint(0, 400) - position_y = randint(0, 300) - rectangle = canva.create_rectangle(position_x, position_y, position_x+taille, - position_y+taille, fill = couleur) - -fenetre = tk.Tk() - -canva = tk.Canvas(fenetre, width = 400, height = 300, bg = "white") -canva.grid() - -#rectangle = canva.create_rectangle(position_x, position_y, position_x+taille,# -# position_y+taille, fill = couleur)# - -Dessin = tk.Button(fenetre, text = "Dessine", command = "spawn_rectangle").grid() - +import tkinter as tk +from random import randint + +def spawn_rectangle() : + indice_couleur = randint(0, 4) + couleur = 0 + if indice_couleur == 0 : + couleur = "red" + if indice_couleur == 1 : + couleur = "green" + if indice_couleur == 2 : + couleur = "blue" + if indice_couleur == 3 : + couleur = "yellow" + if indice_couleur == 4 : + couleur = "grey" + taille = randint(10, 30) + position_x = randint(0, 400) + position_y = randint(0, 300) + while position_x+taille > 400 or position_y+taille > 300: + taille = randint(10, 31) + position_x = randint(0, 400) + position_y = randint(0, 300) + rectangle = canva.create_rectangle(position_x, position_y, position_x+taille, + position_y+taille, fill = couleur) + +fenetre = tk.Tk() + +canva = tk.Canvas(fenetre, width = 400, height = 300, bg = "white") +canva.grid() + +Dessin = tk.Button(fenetre, text = "Dessine", command = spawn_rectangle).grid() + fenetre.mainloop() \ No newline at end of file