简体   繁体   中英

Tkinter: getting coordinates of a mouse drawn rectangle

在解决上一个问题的答案时,我不知道如何获取代表矩形角和其二维尺寸的4个像素的坐标?

Just use the x,y coordinates contained in the event parameter of the on_button_press() and on_button_release() methods:

width = abs(xRelease - xPress)
height = abs(yRelease - yPress)

You can use canvas.bbox(item) to get the coordinates of the bounding box of an item on the canvas. Because your item is a rectangle, the bounding box exactly represents the rectangle.
In your case it would be:

self.canvas.bbox(self.rect)

This returns a tuple containing (x0, y0, x1, y1) in which point 0 is the upper left corner and point 1 is the lower right corner.
You can easily convert these to the four corners and sizes:

Upper left  = x0, y0
Upper right = x1, y0
Lower left  = x0, y1
Lower right = x1, y1

width  = x1-x0
height = y1-y0

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