如何创建拖放界面? [英] How can I create a Drag and Drop interface?

查看:72
本文介绍了如何创建拖放界面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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中拖放吗?正在询问有关在应用程序之间进行拖放的问题,这不是我要问的。

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

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()


推荐答案

Tkinter不直接支持应用程序中的拖放。但是,拖放操作仅需要为按钮单击(< ButtonPress-1> )进行适当的绑定,鼠标在单击按钮时会移动(< B1-Motion> ),以及释放按钮时(< ButtonRelease-1> )。

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>).

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

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

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

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

例如:

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.

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

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

这篇关于如何创建拖放界面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆