2013-11-28 110 views
1

我需要top才能創建tkinter畫布項目的副本,以便可以從原始圖像中拖出圖像的副本。我拖動圖像工作,但我似乎無法複製圖像項目。任何幫助將不勝感激!謝謝。複製Tkinter畫布項目

編輯:對不起,我的代碼一開始。由於給出的答案,我能夠解決問題。下面是我的代碼下調的例子,現在工作:

from tkinter import * 
from PIL import Image, ImageTk 

def OnBaseButtonPress(event): 
    #record the item and its location 
    drag_data["item"] = c.find_closest(event.x, event.y) 

    i = c.itemcget(drag_data["item"], "image") #finds the image source of the object 
    refs.append(i) #keep a reference! 
    c.create_image(c.coords(drag_data["item"]), image=i, tags="base") #creates an identical object at the position 

    drag_data["x"] = event.x 
    drag_data["y"] = event.y 

def OnBaseButtonRelease(event): 
    #reset drag info 
    drag_data["item"] = None 
    drag_data["x"] = 0 
    drag_data["y"] = 0 

def OnBaseMotion(event): 
    #calculate how far the item has moved 
    delta_x = event.x - drag_data["x"] 
    delta_y = event.y - drag_data["y"] 
    #move the object that amount 
    c.move(drag_data["item"], delta_x, delta_y) 
    #record the new position 
    drag_data["x"] = event.x 
    drag_data["y"] = event.y 

#set up canvas and image 
root = Tk() 
c = Canvas(root, width=800, height=600) 
c.pack() 
test = ImageTk.PhotoImage(Image.open("test.png")) 
c.create_image(400, 300, image=test, tags="base") 
refs=[] #used to keep references to images used in functions 

#bind mouse keys 
c.tag_bind("base", "<ButtonPress-1>", OnBaseButtonPress) 
c.tag_bind("base", "<ButtonRelease-1>", OnBaseButtonRelease) 
c.tag_bind("base", "<B1-Motion>", OnBaseMotion) 

drag_data={"x": 0, "y": 0, "item": None} 

mainloop() 
+1

嗨faf。這個問題有點簡短和模糊。請儘可能地提供更多的資料。如果您要求提供代碼,請務必包含您已經嘗試過的代碼,以及爲什麼它不按您希望的方式工作。還請閱讀[寫出完美的問題](http://tinyurl.com/so-hints)。 –

+0

顯示您的代碼。 – furas

回答