栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何创建拖放界面?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何创建拖放界面?

Tkinter不直接支持应用程序中的拖放。但是,拖放操作只需要为按钮单击(

<ButtonPress-1>
),单击按钮(
<B1-Motion>
)和释放按钮(
<ButtonRelease-1>
)时鼠标移动进行适当的绑定即可。

这是一个非常简单的示例,旨在与您的代码一起使用。

首先,我们将创建一个可以管理拖放的类。作为类而不是全局函数的集合更容易做到这一点。

class DragManager():    def add_dragable(self, widget):        widget.bind("<ButtonPress-1>", self.on_start)        widget.bind("<B1-Motion>", self.on_drag)        widget.bind("<ButtonRelease-1>", self.on_drop)        widget.configure(cursor="hand1")    def on_start(self, event):        # you could use this method to create a floating window        # that represents what is being dragged.        pass    def on_drag(self, event):        # you could use this method to move a floating window that        # represents what you're dragging        pass    def on_drop(self, event):        # find the widget under the cursor        x,y = event.widget.winfo_pointerxy()        target = event.widget.winfo_containing(x,y)        try: target.configure(image=event.widget.cget("image"))        except: pass

要使用它,您需要做的就是调用

add_draggable
方法,并为其提供要拖动的小部件。

例如:

label = Label(canvas, image=image)...dnd = DragManager()dnd.add_dragable(label)...root.mainloop()

这就是基本框架的全部内容。由您决定创建一个浮动的可拖动窗口,并突出显示可以放置的项目。

其他实施

有关相同概念的另一个实现,请参见https://github.com/python/cpython/blob/master/Lib/tkinter/dnd.py



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/625095.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号