Python检测另一个形状的形状

时间:2014-06-21 23:44:02

标签: python class tkinter shape

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

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

这可以检测鼠标是否越过块,但是当我在块上拖动其他形状时,它未被检测到。那么当我在块上拖动其他形状时,如何向我发送消息呢? 感谢。

1 个答案:

答案 0 :(得分:0)

此解决方案假定椭圆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")