【问题标题】:tkinter canvas over frame框架上的 tkinter 画布
【发布时间】:2013-07-08 16:06:46
【问题描述】:

我创建了一个拖放程序,可以很好地解决一个小的美学问题。它的工作原理是在列表框中选择一个项目,将其转换为 canvas.create_text,然后将其拖放到画布上。唯一的问题是 create_text 在列表框下,我想知道如何让它出现在列表框的顶部。我尝试更改初始化顺序,并查看了提升/降低,但我没有看到任何变化。

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

【问题讨论】:

  • 我不明白“create_text 在列表框下”是什么意思。当列表框和画布在窗口的两个独立部分不重叠时,它怎么会在列表框下方?
  • 当我单击列表框中的项目时,会在鼠标所在的位置(在列表框中)创建一个 create_text,但列表框位于其顶部。我正在尝试对其进行设置,以便在创建 create_text 时,它会显示在列表框的顶部

标签: canvas listbox tkinter


【解决方案1】:

不,您不能在画布上创建文本,但可以将其显示在其他窗口上方。通常,对于拖放操作,您需要创建一个顶层窗口,并设置 overrideredirect 标志以充当被拖动对象的代理。

【讨论】:

    猜你喜欢
    • 2020-07-10
    • 1970-01-01
    • 1970-01-01
    • 2022-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-17
    • 2021-09-28
    相关资源
    最近更新 更多