解决了为什么脚本只运行一次?

2024-10-04 05:28:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了这个代码,这样当我按下图像时,图像就会改变颜色。问题是点击只起作用一次;当我再次点击时,什么也没发生。为什么

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)

正确:

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)

Tags: andimageeventifbinddefplaceroot