【问题标题】:How to move object on tkinter canvas knowing only its coordinates?如何只知道它的坐标在 tkinter 画布上移动对象?
【发布时间】:2017-02-26 15:41:47
【问题描述】:
for i in range(random.randint(1,4)):
    xos=[150,200,250,300,350,400,450,500,550,600,650,700,750,800,850]
    yos=[150,200,250,300,350,400,450,500,550,600,650]
    xos_=random.choice(xos)
    yos_=random.choice(yos)
    object=canvas.create_image(xos_,yos_,image=postava)
    read_=read.replace("[","").replace("]","").replace("'","").replace("\\n","").replace("\\","")
    loot.write(read_+"\n")

我在 FOR 中的画布上创建图像,我想在需要时删除它们,但只有最后一个具有标记(名称)对象,所以当我输入:canvas.delete(object) 时,它只删除最后一个。所以我想知道是否可以删除某个位置的对象(没有名称或标签)。

【问题讨论】:

  • 在您的标题中,您应该将“move”替换为“remove”,因为您在问题中询问了有关销毁/删除画布对象的问题。

标签: python canvas tkinter tkinter-canvas


【解决方案1】:

当然。 场景(A)假设我们使用鼠标左键在单击时识别画布对象并在释放鼠标左键时删除该对象。

第 1 步:将这些命令包含到您用来绑定到 Button-1 以删除对象的回调/方法中。

mx = canvas.canvasx(event.x) #Translate mouse x screen coordinate to canvas coordinate
my = canvas.canvasy(event.y) #Translate mouse y screen coordinate to canvas coordinate
self.canvasobject = canvas.find_closest(mx, my, halo=5) # get canvas object ID of where mouse pointer is 
print(self.canvasobject) #For you to visualize the canvas object number

第 2 步:将这些命令添加到您用来绑定到 ButtonRelease-1 以删除对象的回调/方法中。

canvas.delete(self.canvasobject) #delete the selected canvas object

场景(B):假设你已经知道画布对象的x,y坐标,可以发出单个命令删除画布对象:

canvas.delete(canvas.find_closest(x, y, halo=5))

请参阅webpage,了解我使用的画布方法和其他画布方法的说明。

【讨论】:

    【解决方案2】:

    如果您知道坐标(坐标),则可以删除画布上的对象。使用item = canvas.find_overlapping(coord) 在该位置查找对象,然后使用canvas.delete(item) 将其删除

    参见下面的示例代码:

    注意:如果您只知道对象上的一个点,则使用canvas.find_overlapping(x, y, x, y) 而不是canvas.find_overlapping(x1, y1, x2, y2)

    import Tkinter as tk
    import random
    
    
    root = tk.Tk()
    canvas = tk.Canvas(root, width=550, height=500, borderwidth=0)
    canvas.pack(expand=True, fill="both")
    
    coord_list=[]
    
    for i in range(random.randint(1,4)):
        xos=[150,200,250,300,350,400,450,500]
        yos=[150,200,250,300,350,400,450]
        xos_=random.choice(xos)
        yos_=random.choice(yos)
    
        coord = (xos_,yos_,xos_+50,yos_+50)
        coord_list.append(coord)
        objectt=canvas.create_rectangle(coord, fill="blue")
    canvas.create_rectangle(25, 15, 50, 40, fill="red")
    
    # Delete red rectangle
    def delete1(event):
        item = canvas.find_overlapping(25, 15, 50, 40)
        canvas.delete(item)
    
    # Delete blue rectangles
    def delete2(event):
        for coord in coord_list:
            item = canvas.find_overlapping(*coord)
            canvas.delete(item)
    
    #Click on the canvas to delete objects at the coordinates
    canvas.bind("<Button-1>", delete1) # change function to delete blue rectangles
    root.mainloop()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-03
      • 1970-01-01
      • 1970-01-01
      • 2021-11-27
      相关资源
      最近更新 更多