【问题标题】:How to get cursor positions on click and drag event in python?如何在python中获取单击和拖动事件的光标位置?
【发布时间】:2020-09-10 11:15:00
【问题描述】:

我想获取所有 光标位置,直到我左键单击并拖动鼠标在 python 中的 tkinter 小部件屏幕上。

下面的代码只提供了鼠标左键单击的位置,而不是鼠标左键事件后拖动鼠标的位置。

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】:

我构建了一个屏幕截图程序,这是其中的一种方法。

  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)

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

这是通过使用 B1-Motion 论证激活的,如下所示。

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

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-06
    • 1970-01-01
    • 2019-01-17
    • 2015-10-18
    • 2013-08-02
    • 2011-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多