Python 悬停或激活时更新Tkinter菜单项中的图像

Python 悬停或激活时更新Tkinter菜单项中的图像,python,user-interface,tkinter,Python,User Interface,Tkinter,我似乎找不到任何关于在鼠标悬停的Tkinter菜单项中更新图像的信息,或者当图像处于活动状态或获得焦点时更新图像的信息。有人知道这是否可行。您的标签上有图像: imageLabel = Label(root,image=image1) imageLabel.pack() def image_two(event): imageLabel["image"] = image2 def image_one(event): imageLabel["image"] = image1 ima

我似乎找不到任何关于在鼠标悬停的Tkinter菜单项中更新图像的信息,或者当图像处于活动状态或获得焦点时更新图像的信息。有人知道这是否可行。

您的标签上有图像:

imageLabel = Label(root,image=image1)
imageLabel.pack()
def image_two(event):
    imageLabel["image"] = image2
def image_one(event):
    imageLabel["image"] = image1
imageLabel.bind("<Enter>", image_two)
imageLabel.bind("<Leave>", image_one)
然后,您需要绑定该函数,以便鼠标在其上移动,留下它来更改图像:

imageLabel = Label(root,image=image1)
imageLabel.pack()
def image_two(event):
    imageLabel["image"] = image2
def image_one(event):
    imageLabel["image"] = image1
imageLabel.bind("<Enter>", image_two)
imageLabel.bind("<Leave>", image_one)

我在windows 10 python 3.6.5上对此进行了测试,效果良好,如果您还有任何问题,请发表评论。

我认为在这种情况下不起作用,因为在进入或离开时,鼠标可能位于菜单项上的文本而不是图像上。您的建议仅在鼠标进入或离开图像时有效,我需要捕获完整菜单项行上的鼠标进入和退出,以便包括文本和图像。我不知道这是否可行。如果标签中有图像,则没有文本。是的,您可以在菜单项中同时包含图像和文本。然后不要这样做!