python - 使用 tkinter 跟踪 Canvas 上每个多边形的每个点

标签 python tkinter canvas coordinates polygon

我正在使用 Python 中的 tkinter 为七巧板(具有不同多边形的拼图)制作 UI,我想在每个多边形在我的 Canvas 上移动时跟踪每个多边形的每个点的坐标。

为此,我创建了此类:

class Polygon:
    def __init__(self, coords, color, canvas):
        self.coords = coords
        self.color = color
        self.move = False

        canvas.bind('<Button-1>', self.start_movement)
        canvas.bind('<Motion>', self.movement)
        canvas.bind('<ButtonRelease-1>', self.stopMovement)
        canvas.bind('<Button-3>', self.rotate)

        canvas.create_polygon(self.coords, fill=self.color)

每个多边形都是这样创建的:

medium_triangle = Polygon((0,0, 100*math.sqrt(2),0, 0,100*math.sqrt(2)),
                          'red', drawing_place)
small_triangle_1 = Polygon((0,0 ,100,0, 0,100), 'purple', drawing_place)
[...]
big_triangle_2 = Polygon((0,0, 200,0, 0,200), 'green', drawing_place)

我的主要问题是,看起来我只能修改最后创建的Polygoncoords 属性。 我使用鼠标在 Canvas 上拖动碎片,然后使用这些方法使我的多边形移动:

def start_movement(self, event):
    self.move = True

    # Translate mouse coordinates to canvas coordinate
    self.initi_x = drawing_place.canvasx(event.x)
    self.initi_y = drawing_place.canvasy(event.y)

    self.movingimage = drawing_place.find_closest(self.initi_x, self.initi_y,
                                                  halo=1)  # get canvas object
                                                           # ID of where mouse
                                                           # pointer is.

def movement(self, event):
    if self.move:

        end_x = drawing_place.canvasx(event.x)  # Translate mouse x screen
                                                # coordinate to canvas coordinate.
        end_y = drawing_place.canvasy(event.y)  # Translate mouse y screen
                                                # coordinate to canvas coordinate.

        deltax = end_x - self.initi_x  # Find the difference
        deltay = end_y - self.initi_y  # Find the difference

        self.newPosition(deltax, deltay)

        self.initi_x = end_x  # Update previous current with new location
        self.initi_y = end_y
        drawing_place.move(self.movingimage, deltax, deltay)  # Move object

def stopMovement(self, event):
    self.move = False
    affichage(self)

我设法将位移添加到我的初始坐标中,这要归功于我的 new_position 方法:

def newPosition(self, deltax, deltay):
    coord = self.coords  # Retrieve object points coordinates
    old_coord = list(coord)  # Tuple to List
    c = []  # New coords
    i = 0  # Cursor on old_coord
    for coordinates in old_coord:

        # check if index of coordinates in range of i and len(old_coord)
        # in old_coord is pair (x coord).
        if (old_coord.index(coordinates, i, len(old_coord)) % 2) == 0:
            c.append(coordinates + deltax)
        else:  # index's impair => y-coord
            c.append(coordinates + deltay)
        i += 1

    coord2 = tuple(c)  # List to Tuple
    self.set_coords(coord2)

 def set_coords(self, coords):
    self.coords = coords

但是正如您在我的控制台中看到的那样

just after medium_triangle declaration:
(0, 0, 141.4213562373095, 0, 0, 141.4213562373095)
(0, 0, 200, 0, 0, 200)
(0, 0, 200, 0, 0, 200)
(0, 0, 200, 0, 0, 200)
(0, 0, 200, 0, 0, 200)
(297.0, 61.0, 497.0, 61.0, 297.0, 261.0)
(551.0, 166.0, 751.0, 166.0, 551.0, 366.0)
(951.0, 250.0, 1151.0, 250.0, 951.0, 450.0)

在声明多边形期间,我似乎能够使用 medium_triangle.coords 打印它们的坐标,但是之后,当我单击 Canvas 时,它会直接显示最后声明的坐标。当我在 Canvas 上移动另一 block 时,它只会添加到相同的多边形

我对类和方法等不太熟悉,但我想我明白我的每个多边形都是我的类的不同实例,但尽管如此,看起来我只能访问Polygon<的一个实例.

我希望我的问题很清楚,我真的创建了不同的多边形吗?如果是,为什么我不能单独修改它们?

最佳答案

canvas.bind('<Button-1>', self.start_movement)
canvas.bind('<Motion>', self.movement)
canvas.bind('<ButtonRelease-1>', self.stopMovement)
canvas.bind('<Button-3>', self.rotate)

您的 Canvas (类型为 Canvas,或者我是这样认为的)只能绑定(bind)到每个键的一个操作。试试这个代码:

canvas.bind('<Button-1>', self.start_movement)
canvas.bind('<Button-1>', lambda e: print("ok"))

...您将看到它不再调用 start_movement(),因为将调用 lambda。 在这里,绑定(bind)到 Canvas 的唯一函数是您最后调用的函数:因此是您创建的最后一个Polygon 的初始化中的函数。通过绑定(bind)新方法,您删除了以前对相同键的绑定(bind)。

关于python - 使用 tkinter 跟踪 Canvas 上每个多边形的每个点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65078241/

相关文章:

python - Azure函数应用程序: Microsoft. Azure.WebJobs.EventHubs : Value cannot be null.(参数 'receiverConnectionString')

python - Pandas : how to get the unique number of values in cells when cells contain lists?

python - 在 Python 中逐行打印输出到 GUI

android - Draw方法中Bitmap的NullPointer

Python读取索引不在列表中的HDF5行

python - Django 应用程序中的线程

python - 在线程中调用 pack_forget()

opencv - 如何在不终止子进程的情况下在被调用进程中使用子进程的输出?

javascript - jQuery 在 Action 发生后加载脚本

c# - 如何检测UI按钮是否已释放?统一-C#