Bonjour,
Je ne comprends pas pourquoi les deux codes ci-dessous ne sont pas équivalents:
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
import tkinter as tk
root = tk.Tk()
can = tk.Canvas(root)
can.grid()
 
def action(event, item):
    print(f'clic sur---> {item}')
 
a = can.create_rectangle(0,0,165,165,
                        fill="white",
                        activefill="yellow",
                        outline="green",
                        width=2,
                        )
b = can.create_rectangle(330,0,165,165,
                     fill="white",
                     activefill="yellow",
                     outline="green",
                     width=2,
                     )
print("item find_all",can.find_all())
can.tag_bind(1, "<1>", lambda event: action(event,1))
can.tag_bind(2, "<1>", lambda event: action(event,2))
Ceci se comporte comme attendu.
Par contre, le code suivant "perd" la liaison de l'événement avec l'item 1:
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
import tkinter as tk
root = tk.Tk()
can = tk.Canvas(root)
can.grid()
 
def action(event, item):
    print(f'clic sur---> {item}')
 
a = can.create_rectangle(0,0,165,165,
                        fill="white",
                        activefill="yellow",
                        outline="green",
                        width=2,
                        )
b = can.create_rectangle(330,0,165,165,
                     fill="white",
                     activefill="yellow",
                     outline="green",
                     width=2,
                     )
print("item find_all",can.find_all())
 
for item in can.find_all():
    can.tag_bind(item, "<1>", lambda event: action(event,item))
Qu'en est il? Merci