如何在tkinter中获得<motion>的方向?

时间:2019-06-01 07:06:25

标签: python tkinter

我正在尝试获取tkinter <Motion>(实际上是<B1-Motion>)事件的方向。

我已经尝试使用event.direction(错误),event.keysym(??),并且事件本身给了我一些坐标。

这是到目前为止的代码:

def holdanddrag(event):
    print('Direction: '+event.   … ) # here I need some help
Widget.bind('<B1-Motion>', holdanddrag)

有人知道该怎么做吗?提前致谢!

2 个答案:

答案 0 :(得分:4)

为了确定方向,您必须存储previous position,并使用它来计算到actual position的运动方向(归一化矢量)。这些变量必须在运动发生时进行更新。

这是一个小示例,它在画布上绘制与鼠标拖动方向相对应的矢量(箭头):

import math
import tkinter as tk
from collections import deque


class Vector:
    """small class for vector arithmetic convenience
    """ 
    def __init__(self, x: float=0, y: float=0) -> None:
        self.x = x
        self.y = y
    def __str__(self) -> str:
        return f'({self.x}, {self.y})'
    def __mul__(self, scalar: float) -> 'Vector':
        return Vector(self.x * scalar, self.y * scalar)
    def magnitude(self) -> float:
        return math.hypot(self.x, self.y)
    def normalize(self) -> 'Vector':
        mag = self.magnitude()
        return Vector(self.x / mag, self.y / mag) if mag != 0 else Vector()
    def __repr__(self) -> str:
        return str(self)
    def __iter__(self) -> float:
        yield self.x
        yield self.y


class Point:
    """small class for point arithmetic convenience
    """ 
    def __init__(self, x: float, y: float):
        self.x = x
        self.y = y        
    def __sub__(self, other: 'Point') -> Vector:
        return Vector(other.x - self.x, other.y - self.y)
    def __add__(self, vec: Vector) -> 'Point':
        return Point(self.x + vec.x, self.y + vec.y)
    def __str__(self) -> str:
        return f'({self.x}, {self.y})'
    def __repr__(self) -> str:
        return str(self)
    def __iter__(self) -> float:
        yield self.x
        yield self.y

def draw_dir(canvas, start_point: Point, _vid=[None]) -> None:
    """draws and updates the scaled normalized direction vector
    on the canvas.
    Keeps track of the id of the canvas item last drawn 
    """
    if _vid[0] is not None:
        canvas.delete(_vid[0])
    normed_scaled_v = direct.normalize() * -50
    end_point = start_point + normed_scaled_v
    _vid[0] = canvas.create_line(*start_point, *end_point, arrow=tk.LAST)

_maxlen = 4

def direction(event, _direct=deque([Vector(0, 0) for _ in range(_maxlen)], maxlen=_maxlen)) -> None:
    """stores previous position, and uses it to calculate the direction
    from the current position.
    updates these variables
    """
    global direct
    _direct.append(Point(event.x, event.y))
    p0, p1 = _direct[0], _direct[-1]
    direct = p1 - p0
    draw_dir(canvas, p1)    
#     print(_direct, direct)

direct = Vector(0, 0)

root = tk.Tk()
canvas = tk.Canvas(root, bg='cyan')
canvas.pack()
canvas.bind('<B1-Motion>', direction)
root.mainloop()

答案 1 :(得分:0)

您可以通过分别用event.x, event.yevent.x_root, event.y_root获取小工具屏幕的坐标来找出方向。

这里是一个示例:

from tkinter import *

def direction(evt):
    print('Screen Coordinates:- x: {}, y: {}'.format(evt.x_root, evt.y_root))
    print('Widget Coordinates:- x: {}, y: {}'.format(evt.x, evt.y))

root = Tk()
root.bind('<B1-Motion>', direction)

root.mainloop()