You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
1.3 KiB
27 lines
1.3 KiB
import tkinter as tk
|
|
from random import randint
|
|
|
|
def spawn_rectangle() :
|
|
"""fonction pour créer un carré de taille et de couleur aléatoire"""
|
|
lst = ["red", "blue", "green", "yellow", "grey"] #liste pour établir les différentes couleurs possibles
|
|
indice_couleur = randint(0, len(lst)-1) #choix de l'indice de lst (pour choisir une couleur) (aléatoire)
|
|
couleur = lst[indice_couleur] #établissement de la couleur choisie
|
|
taille = randint(10, 30) #choix de la taille (aléatoire)
|
|
position_x = randint(0, 390)
|
|
position_y = randint(0, 290) #choix de la position du carré dans le canva (aléatoire)
|
|
if position_x+taille > 400 : #vérification que la taille et la position finale du carré ne dépase pas les limites
|
|
taille = randint(10, 400-position_x) #changement de la position suivant le résultat du booléen ci-dessus
|
|
if position_y+taille > 300 :
|
|
taille = randint(10, 300-position_y)
|
|
rectangle = canva.create_rectangle(position_x, position_y, position_x+taille,
|
|
position_y+taille, fill = couleur) #création du carré
|
|
|
|
fenetre = tk.Tk()
|
|
fenetre.title("Carrés aléatoires")
|
|
|
|
canva = tk.Canvas(fenetre, width = 400, height = 300, bg = "white")
|
|
canva.grid()
|
|
|
|
Dessin = tk.Button(fenetre, text = "Dessine", command = spawn_rectangle).grid()
|
|
|
|
fenetre.mainloop()
|