简体   繁体   English

Tkinter:保存给定宽度的绘制线的坐标

[英]Tkinter: save coordinates of a drawn line of a given width

I have this code where the user can paint using his mouse: 我有以下代码,用户可以在其中使用鼠标进行绘制:

from Tkinter import *

class Test:
   def __init__(self):
       self.b1="up"
       self.xold=None
       self.yold=None
   def test(self,obj):
       self.drawingArea=Canvas(obj)
       self.drawingArea.pack()
       self.drawingArea.bind("<Motion>",self.motion)
       self.drawingArea.bind("<ButtonPress-1>",self.b1down)
       self.drawingArea.bind("<ButtonRelease-1>",self.b1up)
   def b1down(self,event):
       self.b1="down"
   def b1up(self,event):
       self.b1="up"
       self.xold=None
       self.yold=None
   def motion(self,event):
      if self.b1=="down":
           if self.xold is not None and self.yold is not None:
               event.widget.create_line(self.xold,self.yold,event.x,event.y,fill="red",width=4,smooth=TRUE)
           self.xold=event.x
           self.yold=event.y


if __name__=="__main__":
   root=Tk()
   root.wm_title("Test")
   v=Test()
   v.test(root)
   root.mainloop()

I wonder how to save the coordinates of the drawn line knowing that the thickness of the line is 4 (the width can be any integer number less than 10) ? 我不知道知道线的粗细为4 (宽度可以是小于10的任何整数),如何保存绘制线的坐标?

Without the thickness option, the answer is evident for me. 如果没有厚度选项,答案对我来说是显而易见的。

Thank you in advance. 先感谢您。

You cannot get the information you want, if what you want is a list of all the pixels that get changed when you draw a wide line. 如果您想要的是绘制宽线时更改的所有像素的列表,则无法获得所需的信息。 The only information you get when creating a line on the canvas are the coordinates of the endpoints. 在画布上创建线时,您获得的唯一信息是端点的坐标。

If the line is completely horizontal or vertical you can get the bounding box of the line, but that won't work for diagonal lines. 如果该线完全水平或垂直,则可以得到该线的边界框,但不适用于对角线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM