Change cursor when doing a mouse event

最后都变了- 提交于 2021-01-28 07:07:59

问题


How to change the cursor for a mouse event (right click for example) in Python with Tkinter ? When I press the right click, the cursor is changing but when I release it, the cursor doesn't change.

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:


Bind an event to the release of the button which changes the cursor back to "dotbox" using

canevas.bind("<ButtonRelease-3>", self.move_stop)

Then you don't need the <B3-Motion> event, the cursor just stays "fleur" until you release the button


In the code you posted, you need to replace every mention of canevas to self.canevas to be able to reference to it in the move_start function (and any other class methods)



来源:https://stackoverflow.com/questions/28171800/change-cursor-when-doing-a-mouse-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!