解释如何在GTK + 3.0上拖拽

rkm*_*max 5 python-3.x gtk3

大约两个星期我一直在研究GTK +3.0.我想制作一个好的桌面应用程序,但越来越多地发现GTK,更加神秘,我一直在为网络开发,一个半年多的android开发,我有一年的Windows Presentation Foundation经验,从来没有发现这么难要做任何事情,比如GTK,它现在正试图在应用程序中实现拖动文件,最后我找到了一个例子,尽管它完全不能理解某些代码行的需要.

所需的解释是关于方法Gtk.Window.drag_dest_set,Window.on_drag_motionWindow.on_drag_drop

为什么需要使用Gdk.drag_status?为什么我要做什么widget.drag_get_data(context, context.list_targets()[-1], time)

from gi.repository import Gtk, Gdk

class Window(Gtk.Window):
    '''
    Main Window
    '''
    def __init__(self):
        super(Window, self).__init__(title=TITLE)

        self.connect('delete-event', Gtk.main_quit)

        '''
        set up drag
        '''
        self.connect('drag-motion', self.on_drag_motion)
        self.connect('drag-drop', self.on_drag_drop)
        self.connect('drag-data-received', self.on_drag_data_received)
        self.drag_dest_set(0, [], 0)

        self.show()
        Gtk.main()

    def on_drag_motion(self, widgt, context, c, y, time):
        Gdk.drag_status(context, Gdk.DragAction.COPY, time)
        return True

    def on_drag_drop(self, widget, context, x, y, time):
        widget.drag_get_data(context, context.list_targets()[-1], time)

    def on_drag_data_received(self, widget, drag_context, x, y, data, info, time):
        files = data.get_text().rstrip('\n').split('\n')

        for file in files:
            print(file)

if __name__ == '__main__':
    Window()
Run Code Online (Sandbox Code Playgroud)

Col*_*nic 0

也许自上一个版本以来有些东西已经改变:http://python-gtk-3-tutorial.readthedocs.org/en/latest/drag_and_drop.html

一开始有一个关于在特定情况下使用某些方法的小简介。