Reputation: 1077
I created a drag and drop program that works fine with one small aesthetic issue. It works by selecting an item in a listbox, converting that to a canvas.create_text, and then dropping that onto the canvas. The only issue is that the create_text is under the listbox, and I was wondering how to make it appear on top of the listbox. I've tried changing the order of initialization, and looked at raise/lower, but I've seen no change.
def body(self, master):
self.list = Listbox(master, width=26, height=10)
self.list.grid(row=0, column=1)
self.canvas = Canvas(master, width=350, height=500)
self.canvas.grid(row=0, column=0)
for j in self.items:
self.list.insert(END, j)
self.list.bind("<ButtonPress-1>", self.onPress)
self.list.bind("<ButtonRelease-1>", self.onRelease)
self.list.bind("<B1-Motion>", self.onMotion)
def onPress(self, event):
'''Being drag of an object'''
# record the item and its location
w = event.widget
index = w.nearest(event.y)
name = self.list.get(index)
t = self.canvas.create_text(event.x+350, event.y+90, text=name.replace(' ', '\n'),justify=CENTER, tags='sweden')
self.dragData["item"] = t
self.dragData["x"] = event.x
self.dragData["y"] = event.y
self.list.delete(index)
def onMotion(self, event):
'''Handle dragging of an object'''
# compute how much this object has moved
deltaX = event.x - self.dragData["x"]
deltaY = event.y - self.dragData["y"]
# move the object the appropriate amount
self.canvas.move(self.dragData["item"], deltaX, deltaY)
# record the new position
self.dragData["x"] = event.x
self.dragData["y"] = event.y
def onRelease(self, event):
'''End drag of an object'''
# reset the drag information
wx, wy = self.canvas.winfo_rootx(), self.canvas.winfo_rooty()
x,y = self.winfo_pointerxy()
cx = self.canvas.canvasx(x-wx)
cy = self.canvas.canvasy(y-wy)
#Rest of the release code
Upvotes: 1
Views: 441
Reputation: 385970
No, you cannot create text on a canvas but have it appear above some other window. Typically, for a drag and drop operation you will need to create a toplevel window with the overrideredirect flag set to act as the proxy of the object being dragged.
Upvotes: 1