简体   繁体   中英

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) ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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