画一条线或一系列线并得到线的总长度?

Draw a line or a series of lines and get the total length of the lines combined?

我希望我能把我的想法解释清楚,以便你能帮我弄清楚。

假设我点击开始,程序启动 "recording" 鼠标事件,然后我点击并创建一个点,点击其他地方并创建另一个点,当创建第二个点时绘制一条线连接这两个点,依此类推,直到我创建我想要的最后一个点并单击“停止”,然后我得到 return 线的总长度。

在 Python 中完成这项工作有多难?有图书馆可以帮助我实现这样的事情吗?

有没有办法做到这一点完全没有点?只要点击再点击就可以画出一条线等等?

不需要额外的库。您可以简单地将 <Button-1> 绑定到事件并使用 event.xevent.y.

计算距离
from tkinter import *
from random import randint

root = Tk()
root.configure(background='DeepSkyBlue4')

class DrawLine:
    def __init__(self,master):
        self.canvas = Canvas(master, width=500, height=500,bg="white")
        self.canvas.bind("<Button-1>", lambda e: self._move(e.x,e.y))
        self.previous_pos = None
        self.total_length = 0
        self.t = Label(master, text=f"Total Length: {self.total_length} pixels",font=('Arial',12),pady=5,bg="DeepSkyBlue4",fg="white")
        self.t.pack()
        self.canvas.pack()
        self.random_position()

    def _move(self,new_x,new_y):
        self.canvas.create_oval(new_x + 5, new_y + 5, new_x - 5, new_y - 5, width=0, fill='red')
        if self.previous_pos:
            old_x, old_y = self.previous_pos
            self.canvas.create_line(old_x, old_y, new_x, new_y, width=2)
            self.total_length += ((new_x - old_x) ** 2 + (new_y - old_y) ** 2) ** (1 / 2)
            self.t.config(text=f"Total Length: {round(self.total_length,2)} pixels")
        self.previous_pos = (new_x, new_y)

    def random_position(self):
        self._move(randint(0,500),randint(0,500))
        root.after(1000,self.random_position)

DrawLine(root)

root.mainloop()