我目前正在尝试使用tkinter中的标签制作按钮。这些按钮具有“Enter”,“Leave”和“Button-1”绑定。当用户将鼠标悬停在标签上时,图像会在他离开标签时发生变化并恢复正常。当他点击时,事件标签会更改其图像并将其他标签的图像设置为“正常”,但不知何故它不起作用。单击标签后,如果我单击另一个标签并将鼠标悬停在第一个标签上,图像将按预期更改一次,并带有“Enter”绑定,但不会在“离开”时更改。我正在使用Python 3.6以及tkinter和PIL来处理图像。
以下是相关功能:
def changeImage(img_loc, container, width, height) :
img = PIL.Image.open(img_loc)
img = img.resize((width, height), PIL.Image.ANTIALIAS)
img = PIL.ImageTk.PhotoImage(img)
container.config(image = img)
container.image = img
这是它调用的changeImage函数:
lbtn_npcs = generateImage(dir + '/resources/images/buttons/npcs.png', 204, 100, fr_top, bg = '#252A29', x = '1054', y = '0', cur = 'hand2')
lbtn_enemies = generateImage(dir + '/resources/images/buttons/enemies.png', 204, 100, fr_top, bg = '#252A29', x = '830', y = '0', cur = 'hand2')
lbtn_players = generateImage(dir + '/resources/images/buttons/players.png', 204, 100, fr_top, bg = '#252A29', x = '606', y = '0', cur = 'hand2')
buttons_top = [lbtn_npcs, lbtn_enemies, lbtn_players]
pics = [dir + "/resources/images/buttons/npcs.png", dir + "/resources/images/buttons/enemies.png", dir + "/resources/images/buttons/players.png"]
pics_hover = [dir + "/resources/images/buttons/npcs_hover.png", dir + "/resources/images/buttons/enemies_hover.png", dir + "/resources/images/buttons/players_hover.png"].
lbtn_npcs.bind("<Enter>", lambda event : changeImage(dir + "/resources/images/buttons/npcs_hover.png", lbtn_npcs, 204, 100))
lbtn_npcs.bind("<Leave>", lambda event : changeImage(dir + "/resources/images/buttons/npcs.png", lbtn_npcs, 204, 100))
lbtn_npcs.bind("<Button-1>", lambda event : changeBtnPic(buttons_top, pics, pics_hover, 204, 100, event))
lbtn_enemies.bind("<Enter>", lambda event : changeImage(dir + "/resources/images/buttons/enemies_hover.png", lbtn_enemies, 204, 100))
lbtn_enemies.bind("<Leave>", lambda event : changeImage(dir + "/resources/images/buttons/enemies.png", lbtn_enemies, 204, 100))
lbtn_enemies.bind("<Button-1>", lambda event : changeBtnPic(buttons_top, pics, pics_hover, 204, 100, event))
lbtn_players.bind("<Enter>", lambda event : changeImage(dir + "/resources/images/buttons/players_hover.png", lbtn_players, 204, 100))
lbtn_players.bind("<Leave>", lambda event : changeImage(dir + "/resources/images/buttons/players.png", lbtn_players, 204, 100))
lbtn_players.bind("<Button-1>", lambda event : changeBtnPic(buttons_top, pics, pics_hover, 204, 100, event))
def generateImage(img_loc, width, height, container, **kwargs) :
bg = kwargs.get('bg')
x = kwargs.get('x')
y = kwargs.get('y')
img = PIL.Image.open(img_loc)
img = img.resize((width, height), PIL.Image.ANTIALIAS)
img = PIL.ImageTk.PhotoImage(img)
lb_img = tk.Label(container, image = img, bg = "#252A29")
if bg :
lb_img.config(bg = bg)
lb_img.image = img
lb_img.pack()
if x and y :
lb_img.place(x = int(x), y = int(y))
return(lb_img)
以下是标签创建和绑定:
{{1}}
我认为重新绑定不起作用,但我不知道这是不是原因,也不知道如何修复它。你有任何想法或线索吗?
谢谢!
答案 0 :(得分:0)
尝试使用partial,而不是使用lambda。在代码顶部添加from functools import partial
,并替换所有调用,例如
lambda event : changeImage(dir + "/resources/images/buttons/players_hover.png", lbtn_players, 204, 100)
带
partial(change_image, dir + "/resources/images/buttons/players_hover.png", lbtn_players, 204, 100)
下面的链接应该向您解释为什么这有效(假设我已正确识别问题)。 How does the functools partial work in Python?