6

For about two weeks I have been studying GTK +3.0. I want to make a good desktop application, but increasingly find GTK, more cryptic, I have always developed for the web, a little more than six months android development and I have a year of experience with Windows Presentation Foundation and had never found so hard to do anything, like GTK, it was now trying to implement a drag files within an application and in the end I found an example and although it works not understand completely the need for some lines of code.

the required explanation is about the methods Gtk.Window.drag_dest_set, Window.on_drag_motion and Window.on_drag_drop

Why is necesary use Gdk.drag_status? Why i necesary doing 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()
rkmax
  • 17,633
  • 23
  • 91
  • 176
  • Good question, and it's surprising how the example lets you drag within the app, but not drag files over. If you want to know what `Gtk.Window.drag_dest_set` and similar do, run `Gtk.Window.drag_dest_set.__doc__` to see what parameters it expects. – NoBugs Oct 24 '15 at 05:37

1 Answers1

0

Perhaps something has changed since the previous version: http://python-gtk-3-tutorial.readthedocs.org/en/latest/drag_and_drop.html

There is a small blurb in the beginning about using certain methods in particular cases.

Colonel Panic
  • 1,604
  • 2
  • 20
  • 31