Python 如何获得<;动议>;在特金特?
我正在尝试了解tkinterPython 如何获得<;动议>;在特金特?,python,tkinter,Python,Tkinter,我正在尝试了解tkinter(实际上是)事件的方向 我已经尝试使用event.direction(错误),event.keysym(?)和事件本身给了我一些坐标 这是我到目前为止的代码: def holdanddrag(event): print('Direction: '+event. … ) # here I need some help Widget.bind('<B1-Motion>', holdanddrag) def保持和拖动(事件): 打印(‘方向:’+事
(实际上是
)事件的方向
我已经尝试使用event.direction
(错误),event.keysym
(?)和事件本身给了我一些坐标
这是我到目前为止的代码:
def holdanddrag(event):
print('Direction: '+event. … ) # here I need some help
Widget.bind('<B1-Motion>', holdanddrag)
def保持和拖动(事件):
打印(‘方向:’+事件……)#这里我需要一些帮助
Widget.bind(“”,holdanddrag)
有人知道该怎么做吗?提前谢谢 为了确定方向,您必须存储
以前的位置
,并使用它来计算运动方向(标准化向量)到实际位置
。这些变量必须在运动发生时更新
下面是一个小示例,它在画布上绘制了与鼠标拖动方向相对应的向量(箭头):
导入数学
将tkinter作为tk导入
从集合导入deque
类向量:
“用于向量运算方便的小型类”
"""
def uuu init uuuu(self,x:float=0,y:float=0)->无:
self.x=x
self.y=y
定义(自我)->str:
返回f'({self.x},{self.y})'
def uuu mul uuu(自,标量:浮点)->“向量”:
返回向量(self.x*标量,self.y*标量)
def量值(自身)->浮动:
返回math.hypot(self.x,self.y)
def规格化(自)->“向量”:
mag=自身大小()
返回向量(自x/mag,自y/mag)(如果mag!=0其他向量()
定义报告(自我)->str:
返回str(self)
定义(自)->浮动:
屈服自我
屈服于自我
课程点:
“小班,方便点运算”
"""
定义初始化(self,x:float,y:float):
self.x=x
self.y=y
def _uusub _uuu(self,other:'Point')->向量:
返回向量(other.x-self.x,other.y-self.y)
定义添加(自身,向量:向量)->“点”:
返回点(self.x+vec.x,self.y+vec.y)
定义(自我)->str:
返回f'({self.x},{self.y})'
定义报告(自我)->str:
返回str(self)
定义(自)->浮动:
屈服自我
屈服于自我
def draw_dir(画布,起点:点,_vid=[None])->None:
“”“绘制并更新缩放的规格化方向向量
在画布上。
跟踪上次绘制的画布项的id
"""
如果_vid[0]不是无:
canvas.delete(_vid[0])
normed_scaled_v=direct.normalize()*-50
结束点=开始点+标准点
_vid[0]=画布。创建线(*起点,*终点,箭头=tk.LAST)
_maxlen=4
def方向(事件,_direct=deque([Vector(0,0)表示_inrange(_maxlen)],maxlen=_maxlen))->无:
“”“存储以前的位置,并使用它计算方向。”
从当前位置。
更新这些变量
"""
全球直接
_直接追加(点(event.x,event.y))
p0,p1=_direct[0],_direct[-1]
直接=p1-p0
绘制方向(画布,p1)
#打印(_direct,direct)
直接=向量(0,0)
root=tk.tk()
canvas=tk.canvas(根,bg='cyan')
canvas.pack()
画布绑定(“”,方向)
root.mainloop()
为了确定方向,您必须存储一个以前的位置
,并使用它计算到实际位置的运动方向(标准化向量)。这些变量必须在运动发生时更新
下面是一个小示例,它在画布上绘制了与鼠标拖动方向相对应的向量(箭头):
导入数学
将tkinter作为tk导入
从集合导入deque
类向量:
“用于向量运算方便的小型类”
"""
def uuu init uuuu(self,x:float=0,y:float=0)->无:
self.x=x
self.y=y
定义(自我)->str:
返回f'({self.x},{self.y})'
def uuu mul uuu(自,标量:浮点)->“向量”:
返回向量(self.x*标量,self.y*标量)
def量值(自身)->浮动:
返回math.hypot(self.x,self.y)
def规格化(自)->“向量”:
mag=自身大小()
返回向量(自x/mag,自y/mag)(如果mag!=0其他向量()
定义报告(自我)->str:
返回str(self)
定义(自)->浮动:
屈服自我
屈服于自我
课程点:
“小班,方便点运算”
"""
定义初始化(self,x:float,y:float):
self.x=x
self.y=y
def _uusub _uuu(self,other:'Point')->向量:
返回向量(other.x-self.x,other.y-self.y)
定义添加(自身,向量:向量)->“点”:
返回点(self.x+vec.x,self.y+vec.y)
定义(自我)->str:
返回f'({self.x},{self.y})'
定义报告(自我)->str:
返回str(self)
定义(自)->浮动:
屈服自我
屈服于自我
def draw_dir(画布,起点:点,_vid=[None])->None:
“”“绘制并更新缩放的规格化方向向量
在画布上。
跟踪上次绘制的画布项的id
"""
如果_vid[0]不是无:
canvas.delete(_vid[0])
normed_scaled_v=direct.normalize()*-50
结束点=开始点+标准点
_vid[0]=画布。创建线(*起点,*终点,箭头=tk.LAST)
_maxlen=4
def方向(事件,_direct=deque([Vector(0,0)表示_inrange(_maxlen)],maxlen=_maxlen))->无:
“”“存储以前的位置,并使用它计算方向。”
从当前位置。
更新这些变量
"""
全球直接
_直接追加(点(event.x,event.y))
p0,p1=_direct[0],_direct[-1]
直接=p1-p0
绘制方向(画布,p1)
#打印(_direct,direct)
直接=向量(0,0)
root=tk.tk()
canvas=tk.canvas(根,bg='cyan')
canvas.pack()
画布绑定(“”,方向)
root.mainloop()
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()