执行鼠标事件时更改光标

时间:2015-01-27 13:35:22

标签: python-3.x tkinter

如何使用Tkinter在Python中更改鼠标事件的光标(例如右键单击)?当我按下右键时,光标正在改变,但是当我释放它时,光标不会改变。

class App(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.new_width=800
        self.new_height=600

        # Main window
        self.fen = Tk()
        self.fen.title("Image")

        canevas = Canvas(self.fen, bd=0) ## creation of the canvas
        canevas.config(bg="white")

        canevas.config(cursor="dotbox")

        canevas.pack(fill=BOTH,expand=True) ## I place the canvas with the .pack() method

        canevas.config(scrollregion=canevas.bbox("all"))

        w=canevas.winfo_width()
        h=canevas.winfo_height()

        self.fen.update()

        # This is what enables using the mouse:
        canevas.bind("<ButtonPress-3>", self.move_start)
        canevas.bind("<B3-Motion>", self.move_move)

        # start :
    def run(self):
        self.fen.mainloop()

    #move
    def move_start(self,event):
        self.canevas.config(cursor="fleur")

    def move_move(self,event):
        self.canevas.config(cursor="fleur")

1 个答案:

答案 0 :(得分:1)

将事件绑定到释放按钮,使用

将光标更改回"dotbox"
canevas.bind("<ButtonRelease-3>", self.move_stop)

然后你不需要<B3-Motion>事件,光标只停留"fleur",直到你释放按钮


在您发布的代码中,您需要将canevas的每个提及替换为self.canevas,以便能够在move_start函数(以及任何其他类方法)中引用它< / p>