j'ai crée un carré qui bouge tout seul, et je voudrais qu'il ne dépasse pas l'ecran, comment faire ? j'ai ceci comme code :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from tkinter import *
from random import *
 
def Mouvement():
    global dx, dy, cel_nb
    j = len(cel_nb)
 
    for i in range(0, j):
        co[i] = c.coords(cel_nb[i])
        k[i] = randint(0,8)
        print(k)
 
        if co[i][2] <= 600 and ( k[i] == 1 or k[i] == 5 ):
            dx = pas
 
        if co[i][0] >= 0 and ( k[i] == 2 or k[i] == 6 ) :
            dx = -pas
 
        if co[i][1] <= 600 and ( k[i] == 3 or k[i] == 7 ) :
            dy = pas
 
        if co[i][0] >= 0 and ( k[i] == 4 or k[i] == 8 ) :
            dy = -pas
        print(co[0])
    c.move(cel_nb[i], dx, dy)
    f.after(100, Mouvement)
 
def Cellule():
    x1, x2 = 10, 20
    n = 1
    cel_one = randint(1,n)
    for i in range(0, cel_one):
        cel_nb.append([c.create_rectangle(x1, 10, x2, 20, fill = "white", outline = "white")])
        x1 += 40
        x2 += 40
    j = len(cel_nb)
    for i in range(0,j) :
        k.append(i)
        co.append(i)
    print('k = ', k)
 
 
f = Tk()
                ##--Variables--##
 
pas = 10
dx, dy = pas, pas
cel_nb = []
k = []
co = []
 
c = Canvas(f, bg='dark grey', width = 800, height = 600)
c.pack()
Cellule()
print('nb de cellule = ', cel_nb)
Mouvement()
f.mainloop()
Merci pour vos futur réponse.