Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何查找行程Tkinter按钮按_Python_Button_Tkinter_Drag - Fatal编程技术网

Python 如何查找行程Tkinter按钮按

Python 如何查找行程Tkinter按钮按,python,button,tkinter,drag,Python,Button,Tkinter,Drag,我可以跟踪用户单击的位置和发布的位置,但我想跟踪移动的距离 从Tkinter进口* root=Tk() 类DragCursor(): def\uuuuu init\uuuuuu(自身,位置): self.label=位置 位置绑定(“”,self.StartMove) location.bind(“”,self.StopMove) def StartMove(自身、事件): startx=event.x starty=event.y 打印[startx,starty] def停止移动(自身、事件

我可以跟踪用户单击的位置和发布的位置,但我想跟踪移动的距离

从Tkinter进口* root=Tk()

类DragCursor():

def\uuuuu init\uuuuuu(自身,位置):
self.label=位置
位置绑定(“”,self.StartMove)
location.bind(“”,self.StopMove)
def StartMove(自身、事件):
startx=event.x
starty=event.y
打印[startx,starty]
def停止移动(自身、事件):
self.StartMove
stopx=event.x
stopy=event.y
打印[stopx,stopy]
位置=画布(根,宽度=300,高度=300)
拖缆器(位置)
location.pack()
root.mainloop()

您只需要使用距离公式来确定xy平面中两点之间的距离

此外,还需要包含某种实例变量,用于保存起点和终点的坐标,以便在释放鼠标后进行计算

这几乎就是您的代码,只需在
StopMove
末尾使用
self.positions
打印一个新的
distancetraveled
函数即可

from Tkinter import *

root = Tk()

class DragCursor():
    def __init__(self, location):
        self.label = location
        location.bind('<ButtonPress-1>', self.StartMove)
        location.bind('<ButtonRelease-1>', self.StopMove)
        self.positions = {}

    def StartMove(self, event):
        startx = event.x
        starty = event.y
        self.positions['start'] = (startx, starty)

    def StopMove(self, event):
        stopx = event.x
        stopy = event.y
        self.positions['stop'] = (stopx, stopy)
        print self.distancetraveled()

    def distancetraveled(self):
        x1 = self.positions['start'][0]
        x2 = self.positions['stop'][0]
        y1 = self.positions['start'][1]
        y2 = self.positions['stop'][1]
        return ((x2-x1)**2 + (y2-y1)**2)**0.5

location = Canvas(root, width = 300, height = 300)
DragCursor(location)
location.pack()
root.mainloop()
从Tkinter导入*
root=Tk()
类DragCursor():
定义初始化(自身,位置):
self.label=位置
位置绑定(“”,self.StartMove)
location.bind(“”,self.StopMove)
self.positions={}
def StartMove(自身、事件):
startx=event.x
starty=event.y
self.positions['start']=(startx,starty)
def停止移动(自身、事件):
stopx=event.x
stopy=event.y
自身位置['stop']=(stopx、stopy)
打印self.distance()
def距离(自身):
x1=自身位置['start'][0]
x2=自身位置['stop'][0]
y1=自身位置['start'][1]
y2=自身位置['stop'][1]
返回((x2-x1)**2+(y2-y1)**2)**0.5
位置=画布(根,宽度=300,高度=300)
拖缆器(位置)
location.pack()
root.mainloop()

您只需要使用距离公式来确定xy平面中两点之间的距离

此外,还需要包含某种实例变量,用于保存起点和终点的坐标,以便在释放鼠标后进行计算

这几乎就是您的代码,只需在
StopMove
末尾使用
self.positions
打印一个新的
distancetraveled
函数即可

from Tkinter import *

root = Tk()

class DragCursor():
    def __init__(self, location):
        self.label = location
        location.bind('<ButtonPress-1>', self.StartMove)
        location.bind('<ButtonRelease-1>', self.StopMove)
        self.positions = {}

    def StartMove(self, event):
        startx = event.x
        starty = event.y
        self.positions['start'] = (startx, starty)

    def StopMove(self, event):
        stopx = event.x
        stopy = event.y
        self.positions['stop'] = (stopx, stopy)
        print self.distancetraveled()

    def distancetraveled(self):
        x1 = self.positions['start'][0]
        x2 = self.positions['stop'][0]
        y1 = self.positions['start'][1]
        y2 = self.positions['stop'][1]
        return ((x2-x1)**2 + (y2-y1)**2)**0.5

location = Canvas(root, width = 300, height = 300)
DragCursor(location)
location.pack()
root.mainloop()
从Tkinter导入*
root=Tk()
类DragCursor():
定义初始化(自身,位置):
self.label=位置
位置绑定(“”,self.StartMove)
location.bind(“”,self.StopMove)
self.positions={}
def StartMove(自身、事件):
startx=event.x
starty=event.y
self.positions['start']=(startx,starty)
def停止移动(自身、事件):
stopx=event.x
stopy=event.y
自身位置['stop']=(stopx、stopy)
打印self.distance()
def距离(自身):
x1=自身位置['start'][0]
x2=自身位置['stop'][0]
y1=自身位置['start'][1]
y2=自身位置['stop'][1]
返回((x2-x1)**2+(y2-y1)**2)**0.5
位置=画布(根,宽度=300,高度=300)
拖缆器(位置)
location.pack()
root.mainloop()

代码示例的缩进出错。代码示例的缩进出错。