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