【问题标题】:tkinter - Why is there such a thing like bbox?tkinter - 为什么会有像 bbox 这样的东西?
【发布时间】:2020-10-24 00:07:41
【问题描述】:

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

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

对比tcl官方描述here

bbox

pathName bbox tagOrId ?tagOrId tagOrId ...?

返回一个包含四个元素的列表,给出一个近似的边界框 对于由 tagOrId 参数命名的所有项目。该列表具有 形成“x1 y1 x2 y2”,使得所有命名的绘制区域 元素位于左侧 x1 和 x2 所界定的区域内 对,y1 在顶部,y2 在底部。返回值可能 高估实际边界框几个像素。如果没有项目 匹配任何 tagOrId 参数,或者匹配项是否为空 边界框(即它们没有可显示的内容)然后是空的 返回字符串。

坐标

pathName coords tagOrId ?coordList?

查询或修改定义项目的坐标。如果没有坐标 指定时,此命令返回一个列表,其元素是 由 tagOrId 命名的项目的坐标。如果坐标是 指定,然后它们替换命名的当前坐标 物品。如果 tagOrId 引用了多个项目,那么第一个 使用显示列表。

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

【问题讨论】:

    标签: python tkinter tkinter-canvas


    【解决方案1】:

    不同之处在于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)

    坐标

    [10.0, 10.0, 30.0, 50.0]

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

    【讨论】:

      【解决方案2】:

      了解bbox

      让我们在这里使用这段代码:

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

      然后运行这个返回到这个:

      如果您点击蓝色矩形,将打印出以下内容:

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

      点击绿色时会打印:

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

      所以 bbox 只是静默,然后比较坐标值并返回给我们一个列表。喜欢:

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

      打印出来的 bbx 将是:

      [9.0, 19.0, 81.0, 81.0]
      

      我们从上面知道。

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

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

      结果如下:

      【讨论】:

        猜你喜欢
        • 2013-05-02
        • 1970-01-01
        • 2013-07-30
        • 2014-09-29
        • 1970-01-01
        • 1970-01-01
        • 2023-03-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多