'Why script run only 1 time?
I wrote this code so that when I press on the image this image changes color. The problem is that click works only once; when I click a second time, nothing happens. Why?
immagine1 = Label(root, image=photo1)
immagine1.place(x=20, y=20)
def su1(event):
print ("coordinate 1", event.x, event.y)
clickX1 = event.x
clickY1 = event.y
if (clickX1 >= 10 and clickX1 <= 275 and clickY1 >= 10 and clickY1 <= 320):
immagine1 = Label(root, image=photo2)
immagine1.place(x=20, y=20)
def giu1(event):
clickX1 = event.x
clickY1 = event.y
if (clickX1 >= 10 and clickX1 <= 275 and clickY1 >= 10 and clickY1 <= 320):
immagine1 = Label(root, image=photo1)
immagine1.place(x=20, y=20)
immagine1.bind("<Button-1>", su1)
immagine1.bind("<ButtonRelease-1>", giu1)
Correct:
immagine1 = Label(root, image=photo1)
immagine1.place(x=20, y=20)
def su1(event):
print ("coordinate 1", event.x, event.y)
clickX1 = event.x
clickY1 = event.y
if (clickX1 >= 10 and clickX1 <= 275 and clickY1 >= 10 and clickY1 <= 320):
immagine1.configure(image=photo2)
def giu1(event):
clickX1 = event.x
clickY1 = event.y
if (clickX1 >= 10 and clickX1 <= 275 and clickY1 >= 10 and clickY1 <= 320):
immagine1.configure(image=photo1)
immagine1.bind("<Button-1>", su1)
immagine1.bind("<ButtonRelease-1>", giu1)
Solution 1:[1]
The problem seems to be this line:
immagine1 = Label(root, image=photo2)
Here you're re-assigning the immagine1
variable and therefore you're losing the bind that you did:
immagine1.bind("<Button-1>", su1)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | 0x32e0edfb |