tkinter - 为什么会有像 bbox 这样的东西? [英] tkinter - Why is there such a thing like bbox?

查看:38
本文介绍了tkinter - 为什么会有像 bbox 这样的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我更多地使用 tkinter Canvas,我想知道 bbox 的使用.

对我来说,我使用 bbox 来获取元素的坐标,但 Canvas 已经有一种方法可以返回项目的坐标.那么他们为什么要发明像 bbox 这样的东西?

对比官方 tcl 描述

如果您单击蓝色矩形,则会打印出以下内容:

1 被点击bbox 返回, (9, 19, 81, 81)坐标返回,[10.0, 20.0, 50.0, 50.0]

点击绿色将打印:

2 被点击bbox 返回, (9, 19, 81, 81)坐标返回,[30.0, 30.0, 80.0, 80.0]

所以 bbox 只是安静,然后比较坐标值并返回一个列表.喜欢:

导入 tkinter 作为 tkdef rectangel_us(canvas, *items):coords = {x1":[],y1":[],x2":[],y2":[]}对于 i 在项目中:coords['x1'].append(canvas.coords(i)[0])coords['y1'].append(canvas.coords(i)[1])coords['x2'].append(canvas.coords(i)[2])coords['y2'].append(canvas.coords(i)[3])x1 = min(coords['x1'])-1y1 = min(coords['y1'])-1x2 = max(coords['x2'])+1y2 = 最大值(坐标['y2'])+1返回[x1,y1,x2,y2]根 = tk.Tk()c = tk.Canvas(root,width=250,height=250)f = c.create_rectangle(10,20, 50, 50,填充 =蓝色")秒 = c.create_rectangle(30,30, 80, 80,填充 =绿色")bbx = rectangel_us(c, f, sec)打印(bbx)c.pack()root.mainloop()

打印出来的 bbx 将是:

[9.0, 19.0, 81.0, 81.0]

正如我们从上面知道的.

这可以通过这里的代码看到:

导入 tkinter 作为 tkdef rectangel_us(canvas, *items):coords = {x1":[],y1":[],x2":[],y2":[]}对于 i 在项目中:coords['x1'].append(canvas.coords(i)[0])coords['y1'].append(canvas.coords(i)[1])coords['x2'].append(canvas.coords(i)[2])coords['y2'].append(canvas.coords(i)[3])x1 = min(coords['x1'])-1y1 = min(coords['y1'])-1x2 = max(coords['x2'])+1y2 = 最大值(坐标['y2'])+1canvas.create_rectangle(x1,y1,x2,y2,大纲='红色')根 = tk.Tk()c = tk.Canvas(root,width=250,height=250)f = c.create_rectangle(10,20, 50, 50,填充 =蓝色")秒 = c.create_rectangle(30,30, 80, 80,填充 =绿色")bbx = rectangel_us(c, f, sec)c.pack()root.mainloop()

结果如下:

Now that I'm working more with tkinter Canvas I was wondering about the use of bbox.

For me I'm using bbox to get the coords of an element but the Canvas already have a method to return the coords of an item. So why did they invent something like bbox?

Comparing the official tcl description 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.

coords

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.

I see the difference between these, but can't imaging in which case I would need a bbox instead of the coords? Can someone teach me a better understanding of this please?

解决方案

Understanding bbox

Lets take this bit of Code here:

import tkinter as tk

def do_bbx(event):
    item_id = event.widget.find_withtag('current')[0]
    crds = event.widget.coords(item_id)
    print(f'{item_id} was clicked')
    print(f'bbox returns, {bbx}')
    print(f'coords returns, {crds}')

root = tk.Tk()
c = tk.Canvas(root,width=250,height=250)
f = c.create_rectangle(10,20, 50, 50,
                       fill = "BLUE")
sec = c.create_rectangle(30,30, 80, 80,
                         fill = "GREEN")
bbx = c.bbox(f, sec)
c.tag_bind('all', "<Button-1>", do_bbx)

c.pack()

root.mainloop()

and run this which return into this:

If you click on the blue rectangle the following will be printed out:

1 was clicked
bbox returns, (9, 19, 81, 81)
coords returns, [10.0, 20.0, 50.0, 50.0]

While clicking on the green will print:

2 was clicked
bbox returns, (9, 19, 81, 81)
coords returns, [30.0, 30.0, 80.0, 80.0]

So bbox does just quiet else then compareing the values of the coordinates and returns us a list. Like:

import tkinter as tk

def rectangel_us(canvas, *items):
    coords = {"x1":[],"y1":[],"x2":[],"y2":[]}
    for i in items:
        coords['x1'].append(canvas.coords(i)[0])
        coords['y1'].append(canvas.coords(i)[1])
        coords['x2'].append(canvas.coords(i)[2])
        coords['y2'].append(canvas.coords(i)[3])
    x1 = min(coords['x1'])-1
    y1 = min(coords['y1'])-1
    x2 = max(coords['x2'])+1
    y2 = max(coords['y2'])+1
    return[x1,y1,x2,y2]

root = tk.Tk()
c = tk.Canvas(root,width=250,height=250)
f = c.create_rectangle(10,20, 50, 50,
                       fill = "BLUE")
sec = c.create_rectangle(30,30, 80, 80,
                         fill = "GREEN")

bbx = rectangel_us(c, f, sec)
print(bbx)


c.pack()

root.mainloop()

the printed out bbx will be:

[9.0, 19.0, 81.0, 81.0]

as we know from above.

This can be visible by this code here:

import tkinter as tk

def rectangel_us(canvas, *items):
    coords = {"x1":[],"y1":[],"x2":[],"y2":[]}
    for i in items:
        coords['x1'].append(canvas.coords(i)[0])
        coords['y1'].append(canvas.coords(i)[1])
        coords['x2'].append(canvas.coords(i)[2])
        coords['y2'].append(canvas.coords(i)[3])
    x1 = min(coords['x1'])-1
    y1 = min(coords['y1'])-1
    x2 = max(coords['x2'])+1
    y2 = max(coords['y2'])+1
    canvas.create_rectangle(x1,y1,x2,y2,
                            outline='red')

root = tk.Tk()
c = tk.Canvas(root,width=250,height=250)
f = c.create_rectangle(10,20, 50, 50,
                       fill = "BLUE")
sec = c.create_rectangle(30,30, 80, 80,
                         fill = "GREEN")

bbx = rectangel_us(c, f, sec)


c.pack()

root.mainloop()

Which result in this:

这篇关于tkinter - 为什么会有像 bbox 这样的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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