Python 如何将画布(行、列)位置的边界框作为网格元组返回
我正在尝试编写一个代码,它最终将如下所示: 画布只是像素的海洋,所以我所要做的就是添加一点额外的功能 让航行在那片海域更容易。稍后,我将在屏幕上绘制矩形(嗯,正方形) 画布来表示实体 为此,我需要一个左上角和右下角的像素坐标。 因此get_bbox允许从Position(x,y)类的实例进行翻译(这是我们喜欢的工作) 由于它存储在我们的网格字典中)中,所以可以将其转换为这些像素坐标,从而使我们可以像网格一样轻松地处理画布Python 如何将画布(行、列)位置的边界框作为网格元组返回,python,tkinter,canvas,grid,Python,Tkinter,Canvas,Grid,我正在尝试编写一个代码,它最终将如下所示: 画布只是像素的海洋,所以我所要做的就是添加一点额外的功能 让航行在那片海域更容易。稍后,我将在屏幕上绘制矩形(嗯,正方形) 画布来表示实体 为此,我需要一个左上角和右下角的像素坐标。 因此get_bbox允许从Position(x,y)类的实例进行翻译(这是我们喜欢的工作) 由于它存储在我们的网格字典中)中,所以可以将其转换为这些像素坐标,从而使我们可以像网格一样轻松地处理画布 class AbstractGrid(tk.Canvas):
class AbstractGrid(tk.Canvas):
def __init__(self, master, rows, cols, width, height, **kwargs):
"""
master: The window in which the grid should be drawn.
rows: The integer number of rows in the grid.
cols: The integer number of columns in the grid.
width: The width of the grid canvas (in pixels).
height: The height of the grid canvas (in pixels).
**kwargs: Any other additional named parameters appropriate to tk.Canvas.
"""
master=self._master
rows=self._rows
cols=self._cols
width=self._width
height=self._height
super().__init__(master, rows, cols, width, height, **kwargs)
def get_bbox(self, position):
"""
Returns the bounding box for the (row, column) position;
this is a tuple containing information about the pixel positions of the edges of the shape,
in the form (x min, y min, x max, y max).
"""
我不知道如何编写这个get_bbox函数。请您帮我介绍一下好吗
谢谢这是简单的数学:
类抽象网格(tk.Canvas):
定义初始值(自、主、行、列、宽度、高度,**kwargs):
"""
主窗口:绘制网格的窗口。
行:网格中的整数行数。
cols:网格中列的整数。
宽度:网格画布的宽度(以像素为单位)。
高度:网格画布的高度(以像素为单位)。
**kwargs:适用于tk.Canvas的任何其他命名参数。
"""
超级()
self.\u行=行
self.\u cols=cols
自身宽度=宽度
自身高度=高度
#计算栅格宽度和高度
自身网格宽度=宽度/列
自身网格高度=高度/行数
def get_bbox(自身,位置):
"""
返回(行、列)位置的边界框;
这是一个元组,包含形状边缘的像素位置信息,
形式为(x最小、y最小、x最大、y最大)。
"""
行,列=位置
x、 y=col*self.\U grid\U w,row*self.\U grid\U h
返回x,y,x+self.\u grid\u w,y+self.\u grid\u h
请注意,我在\uuuu init\uuuu()
中修复了以下几行的问题:
master=self._master#self.master是隐式创建的
行=自身。_行#应在这四行上交换左行和右行
科尔斯=自我
宽度=自身宽度
高度=自身高度
另外请注意,如果希望使用方形网格,最好指定单个网格大小,而不是指定行
,列