【问题标题】:stop canvas shape from following cursor after a mouse click单击鼠标后停止画布形状跟随光标
【发布时间】:2021-05-16 02:18:28
【问题描述】:

我正在制作一个 GUI 来使用 Tkinter 绘制加权图,所以我制作了一个按钮,单击该按钮时使用画布创建一个圆(图顶点)。那么圆圈应该跟随光标到画布上的任何位置,并在鼠标点击时停止。

我设法让圆圈跟随光标,但我不知道如何让它停止跟随。

这是我做的功能

def buttonClick():
    def Mouse_move(event):
        x,y = event.x , event.y
        canvas.moveto(vertex,x,y )

    vertex= canvas.create_oval(650, 100, 750, 200)
    canvas.bind("<Motion>", Mouse_move)

【问题讨论】:

    标签: python tkinter tkinter-canvas


    【解决方案1】:

    您可以在回调中绑定鼠标点击事件&lt;Button-1&gt;和解除绑定&lt;Motion&gt;事件:

    def buttonClick():
        def mouse_move(event):
            x, y = event.x, event.y
            canvas.moveto(vertex, x, y)
    
        def mouse_click(event):
            canvas.unbind("<Motion>")
    
        vertex = canvas.create_oval(650, 100, 750, 200)
        canvas.bind("<Motion>", mouse_move)
        canvas.bind("<Button-1>", mouse_click)
    

    【讨论】: