Tkinter:保存给定宽度的绘制线的坐标 [英] Tkinter: save coordinates of a drawn line of a given width

查看:101
本文介绍了Tkinter:保存给定宽度的绘制线的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码,用户可以用他的鼠标绘画:

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

不知如何保存绘制的线的坐标知道线的粗细是4(宽度可以是小于10的任意整数)?

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.

提前致谢.

推荐答案

如果您想要的是绘制宽线时发生更改的所有像素的列表,则您无法获得所需的信息.在画布上创建线条时,您获得的唯一信息是端点的坐标.

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.

这篇关于Tkinter:保存给定宽度的绘制线的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆