2013-07-08 947 views
1

我創建了一個拖放程序,可以很好地處理一個小的美學問題。它通過選擇列表框中的項目,將其轉換爲canvas.create_text,然後將其放到畫布上。唯一的問題是,create_text在列表框下,我想知道如何使它出現在列表框的頂部。我試着改變初始化的順序,看着提高/降低,但我沒有看到任何改變。tkinter畫布在框架

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 
+0

我不明白「create_text是在listbox下」的意思。當列表框和畫布位於不重疊的窗口的兩個獨立部分時,它如何能在列表框下面? –

+0

當我點擊列表框中的一個項目時,會在鼠標所在的位置(在列表框中)創建一個create_text,但列表框位於其上。我試圖設置它,以便在create_text創建時顯示在列表框的頂部 –

回答

1

不,您不能在畫布上創建文字,但會將其顯示在某個其他窗口的上方。通常,對於拖放操作,您需要創建一個頂層窗口,並將overrideredirect標誌設置爲充當被拖動對象的代理。