如何解决在悬停时向画布形状添加轮廓?
def OnHover(event):
canvas.itemconfig(c1,outline="#3385ff",width = hw)
def UnHover(event):
canvas.itemconfig(c1,outline="")
canvas.tag_bind(shape,'<Enter>',OnHover)
canvas.tag_bind(shape,'<Leave>',UnHover)
我想知道是否可以使用少量代码来赋予多个形状这种效果。 我目前有这个,它相当混乱,可以整理和缩短,但我有点卡住了。
def OnHover1(event):
canvas.itemconfig(c1,width = hw)
def UnHover1(event):
canvas.itemconfig(c1,outline="")
for i in tbh1:
canvas.tag_bind(i,OnHover1)
canvas.tag_bind(i,UnHover1)
def OnHover2(event):
canvas.itemconfig(c4,width = hw)
def UnHover2(event):
canvas.itemconfig(c4,outline="")
for i in tbh2:
canvas.tag_bind(i,OnHover2)
canvas.tag_bind(i,UnHover2)
def OnHover3(event):
canvas.itemconfig(c7,width = hw)
def UnHover3(event):
canvas.itemconfig(c7,outline="")
for i in tbh3:
canvas.tag_bind(i,OnHover3)
canvas.tag_bind(i,UnHover3)
抱歉,如果我措辞不好。
解决方法
我不确定我是否完全理解您的要求,但这是我的想法:
def OnHover(item,hw):
canvas.itemconfig(item,outline="#3385ff",width = hw)
def UnHover(item):
canvas.itemconfig(item,outline="")
def bindList(l,item,hw):
for i in l:
canvas.tag_bind(i,'<Enter>',lambda event: OnHover(item=item,hw=hw))
canvas.tag_bind(i,'<Leave>',lambda event: UnHover(item=item))
bindList(tbh1,c1,hw)
bindList(tbh2,c4,hw)
bindList(tbh3,c7,hw)
现在有一个 bindList
函数,它遍历列表并标记每个项目。 OnHover 和 UnHover 函数现在采用您想要更改其轮廓的项目。我无法对此进行测试,因为我不知道 c1、c4、tbh1 等是什么或它们包含什么,但这应该可行。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。