from tkinter import * import random fenetre = Tk() canvas = Canvas(fenetre, width=400, height=300, background='white') canvas.grid() def carre(): x1 = random.randint(0,400) largeur_aleatoire = random.randint(10,30) x2 = x1 + largeur_aleatoire y1 = random.randint(0,300) y2 = y1 + largeur_aleatoire couleur = random.choice(["red", "green", "blue", "yellow", "grey"]) if x2 > 400 or y2 > 300 or y1 == 0 or x1 == 0 or y1 == 300 or x1 == 400: while x2 > 400 or y2 >300 or y1 == 0 or x1 == 0 or y1 == 300 or x1 == 400: x1 = random.randint(0,400) x_intermédiaire = random.randint(10,30) x2 = x1 + x_intermédiaire y1 = random.randint(0,300) y2 = y1 + x_intermédiaire couleur = random.choice(["red", "green", "blue", "yellow", "grey"]) else: carré = canvas.create_rectangle(x1, y1, x2, y2, fill= couleur, outline = couleur) canvas.grid() bouton=Button(fenetre, text="Dessine", command= carre) bouton.grid() fenetre.mainloop() fenetre.destroy()