【问题标题】:Python Detect shape over another shapePython在另一个形状上检测形状
【发布时间】:2014-06-21 23:44:02
【问题描述】:

我需要知道如何检测用鼠标拖动的椭圆是否进入另一个形状(矩形)。

class Bloque(): # Creates a block
   def __init__(self, lista, ventana):
       self.espacio = ventana # assings the canvas
       box = self.espacio.create_rectangle(x1, y1, x2, y2, width = 0) # creates the block
       self.espacio.tag_bind(box, '<Enter>', lambda e: print("Passed over"))

这会检测鼠标是否经过块,但是当我将其他形状拖到块上时,它没有被检测到。那么,当我将其他形状拖到块上时,我该如何做到这一点? 谢谢。

【问题讨论】:

    标签: python class tkinter shape


    【解决方案1】:

    此解决方案假定椭圆 ID 存储在 self.oval 中。另请注意,我将盒子 ID 存储在实例变量 self.box 而不是局部变量 box 中。

    class Bloque(): # Creates a block
       def __init__(self, lista, ventana):
           self.espacio = ventana # assings the canvas
           self.box = self.espacio.create_rectangle(x1, y1, x2, y2, width = 0) # creates the block
           self.espacio.tag_bind(self.box, '<B1-Motion>', self.detectIntersection)
    
       def detectIntersection(self, event):
           overlaps = self.espacio.find_overlapping(*self.espacio.bbox(box))
           if self.oval in overlaps:
              print("Passed over")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-21
      • 1970-01-01
      • 1970-01-01
      • 2016-06-01
      • 1970-01-01
      相关资源
      最近更新 更多