Python 如何创建拖放界面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44887576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:35:18  来源:igfitidea点击:

How can I create a Drag and Drop interface?

pythontkinterdrag-and-droppython-3.5

提问by Maxis

Currently, I am working with Python 3.5 GUI development using tkinter module. I want to be able to drag an image from one place to another within the application. Does tkinter support drag and drop within an application, and if so, how do you do it?

目前,我正在使用 tkinter 模块进行 Python 3.5 GUI 开发。我希望能够在应用程序中将图像从一个地方拖到另一个地方。tkinter 是否支持在应用程序内拖放,如果支持,您是如何做到的?

The question Drag and Drop in Tkinter?is asking about dragging and dropping between applications, which is not what I'm asking here.

Tkinter 中的拖放问题正在询问应用程序之间的拖放,这不是我在这里问的。

from tkinter import *

root = Tk()
root.geometry("640x480")

canvas = Canvas(root, height=480, width=640, bg="white")

frame = Frame(root, height=480, width=640, bg="white")
frame.propagate(0)

image = PhotoImage(file="C:/Users/Shivam/Pictures/Paint/Body.png")

label = Label(canvas, image=image)
label.pack()

label_2 = Label(frame, text="Drop Here !")
label_2.pack()
label_2.place(x=200, y=225, anchor=CENTER)

canvas.pack(side=LEFT)
frame.pack()

root.mainloop()

回答by Bryan Oakley

Tkinter doesn't have any direct support for drag and drop within an application. However, drag and drop requires not much more than making suitable bindings for a button click (<ButtonPress-1>), the mouse moving while the button is clicked (<B1-Motion>), and when the button is released (<ButtonRelease-1>).

Tkinter 对应用程序内的拖放没有任何直接支持。但是,拖放只需要为单击按钮 ( <ButtonPress-1>)、单击按钮时移动鼠标 ( <B1-Motion>) 以及释放按钮时( )进行合适的绑定即可<ButtonRelease-1>

Here is a very simplestic example which is designed to work with your code.

这是一个非常简单的示例,旨在处理您的代码。

First, we'll create a class that can manage the dragging and dropping. It's easier to do this as a class rather than a collection of global functions.

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

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

To use it, all you need to do is call the add_draggablemethod, giving it the widget(s) you wish to drag.

要使用它,您需要做的就是调用该add_draggable方法,为其提供您希望拖动的小部件。

For example:

例如:

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

That's all it takes for the basic framework. It's up to you to create a floating draggable window, and to perhaps highlight the item(s) that can be dropped on.

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

Other implementations

其他实现

For another implementation of the same concept, see https://github.com/python/cpython/blob/master/Lib/tkinter/dnd.py

对于相同概念的另一种实现,请参阅https://github.com/python/cpython/blob/master/Lib/tkinter/dnd.py

回答by Maxis

https://github.com/akheron/cpython/blob/master/Lib/tkinter/dnd.py
I tested it and it still seems to work in python 3.6.1, I suggest experimenting with it and making it your own, because it doesn't seem to be officially supported in Tkinter.

https://github.com/akheron/cpython/blob/master/Lib/tkinter/dnd.py
我对它进行了测试,它似乎仍然可以在 python 3.6.1 中工作,我建议尝试使用它并使其成为你自己的,因为Tkinter 似乎没有正式支持它。