如何在python中获取点击和拖动事件的光标位置?

问题描述 投票:0回答:1

我想获得所有的 光标位置,直到我左键拖动 鼠标在 tkinter widget 屏幕上的位置。

下面的代码只提供了左键的位置,没有提供左键事件后拖动鼠标的位置。

import tkinter as tk

root = tk.Tk()

def leftClick(event):
    x, y = event.x, event.y
    print('({}, {}) -> LEFT'.format(x,y))

root.bind('<Button-1>',leftClick)

root.mainloop()

有什么方法可以获得 拖拽 事件

python tkinter mouseevent drag cursor-position
1个回答
1
投票

我建立了一个屏幕剪裁程序,这里是其中的一个方法。

  def on_move_press(self, event):
        self.curX, self.curY = (event.x, event.y)
        # expand rectangle as you drag the mouse
        print(self.curX)
        print(self.curY)
        self.screenCanvas.coords(self.rect, self.start_x, self.start_y, self.curX, self.curY)

当用户拖动snipping矩形时,它将动态更新并打印当前X和当前Y位置的值(直到用户释放)。

如下图所示,通过使用B1-Motion论证来激活这个方法。

self.screenCanvas.bind("<B1-Motion>", self.on_move_press)

希望这能帮助你

© www.soinside.com 2019 - 2024. All rights reserved.