user4473281
user4473281

Reputation:

Change cursor when doing a mouse event

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")

Upvotes: 1

Views: 777

Answers (1)

fhdrsdg
fhdrsdg

Reputation: 10532

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)

Upvotes: 1

Related Questions