python - tkinter - 为什么会有像 bbox 这样的东西?

标签 python tkinter tkinter-canvas

现在我更多地使用 tkinter Canvas,我想知道 bbox 的使用。
对我来说,我使用 bbox 来获取元素的坐标,但 Canvas 已经有一个方法来返回一个项目的坐标。那么他们为什么要发明像 bbox 这样的东西呢?
对比官方tcl描述here :
bbox

pathName bbox tagOrId ?tagOrId tagOrId ...?

Returns a list with four elements giving an approximate bounding box for all the items named by the tagOrId arguments. The list has the form ``x1 y1 x2 y2'' such that the drawn areas of all the named elements are within the region bounded by x1 on the left, x2 on the right, y1 on the top, and y2 on the bottom. The return value may overestimate the actual bounding box by a few pixels. If no items match any of the tagOrId arguments or if the matching items have empty bounding boxes (i.e. they have nothing to display) then an empty string is returned.


坐标

pathName coords tagOrId ?coordList?

Query or modify the coordinates that define an item. If no coordinates are specified, this command returns a list whose elements are the coordinates of the item named by tagOrId. If coordinates are specified, then they replace the current coordinates for the named item. If tagOrId refers to multiple items, then the first one in the display list is used.


我看到了这些之间的区别,但无法成像,在这种情况下我需要一个 bbox 而不是坐标?
有人可以教我更好地理解这一点吗?

最佳答案

不同的是,与bbox()您可以获得一组项目的边界框(使用标签或“全部”),而 coords()返回具有给定标签的第一个项目的坐标。这是一个例子

import tkinter as tk

root = tk.Tk()
canvas = tk.Canvas(root)
canvas.pack()

i1 = canvas.create_rectangle(10, 10, 30, 50, tags='rect')
i2 = canvas.create_rectangle(60, 80, 70, 120, fill='red', tags='rect')

canvas.update_idletasks()
print('bbox', canvas.bbox('rect'))
print('coords', canvas.coords('rect'))
这使

bbox

(9, 9, 71, 121)

coords

[10.0, 10.0, 30.0, 50.0]

bbox()的典型用途之一当您想使用 Canvas 滚动一组小部件时:需要将 Canvas 的滚动区域设置为包括所有 Canvas 内容,因此 canvas.bbox('all')非常有用。参见例如 Adding a scrollbar to a group of widgets in Tkinter (在 onFrameConfigure() 函数中)。

关于python - tkinter - 为什么会有像 bbox 这样的东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62712481/

相关文章:

python - 创建另一个相同类型的对象时,如何复制一个tkinter对象的选项?

Python 在另一个函数返回结果后结束函数

python - doc2vec的余弦相似度不准确

python - 如何为 Tkinter 文本小部件添加书签?

python - 我尝试将 Tkinter 按钮绑定(bind)到键盘上的 Enter 键,但它不起作用

python - Tkinter Canvas 和带网格的滚动条

python - 类型错误 : unhashable type: 'dict' in Networkx random walk code that was previously working

python - 使用 SymPy 消除变量以关联 Python 中的两个函数

python - 删除内容后如何设置 tkinter 条目验证

Python:在文本框中使用鼠标滚轮滚动,而鼠标光标不在文本框中(tkinter)