我正在开发一个小蟒蛇贵,只是为了好玩和学习,我一直在努力改变在画布项光标形状。
我知道这是可以改变光标形状悬停在画布组件时,使用光标=上帆布创作的“无所谓”选项。但我想这样做的只有这个帆布内的物品。
这使得正确的项目:
self.image_obj = canvas.create_image(
self.column_coordinate,
self.row_coordinate,
image=image
)
这并不工作:
self.image_obj = canvas.create_image(
self.column_coordinate,
self.row_coordinate,
image=image,
cursor="hand1"
)
“光标”选项似乎并没有对项目存在,是有办法去解决这个问题?
改变光标的唯一方法是通过改变它是如何呈现在画布上。通过检查每一个移动鼠标无论是项目的边界框里面时,你想让它改过来就可以达到这样的效果。
from tkinter import *
canvas = Canvas(width=200,height=200)
canvas.pack()
rec = canvas.create_rectangle(100,0,200,200,fill="red")#example object
def check_hand(e):#runs on mouse motion
bbox= canvas.bbox(rec)
if bbox[0] < e.x and bbox[2] > e.x and bbox[1] < e.y and bbox[3] > e.y:#checks whether the mouse is inside the boundrys
canvas.config(cursor="hand1")
else:
canvas.config(cursor="")
canvas.bind("<Motion>",check_hand)#binding to motion