pythongtkpygtk

Drop file in Python GUI (GTK)


I would like to write a little app who takes a selected number of file, and store the path of everything for future things.

Basically:

Select mixed (images, audio, video) file from Nautilus (in this case), drag them, drop them in this GUI and get the absolute path of every element to write in a list, for example.

The program itself isn't a problem at all, but i fail in the most simple task, maybe: creating a GUI (i've choosed GTK, but everything will be fine, just need the job done) who accept the elements and store them.

I'm playing around with Glade, but i'm not even sure if is this the right choice.

Can someone help me with the build of this GUI or by pointing out some resource?


Solution

  • Heres a good way to get started with Glade, Gtk and Python:

    http://python-gtk-3-tutorial.readthedocs.io/en/latest/builder.html

    And drag and drop:

    http://python-gtk-3-tutorial.readthedocs.io/en/latest/drag_and_drop.html

    Edit with a small working program:

    #!/usr/bin/env python
    
    import gi
    gi.require_version('Gtk', '3.0')
    from gi.repository import Gtk, Gdk
    import os, sys
    
    
    
    class GUI:
        def __init__(self):
            window = Gtk.Window()
            window.connect('destroy', Gtk.main_quit)
            textview = Gtk.TextView()
            enforce_target = Gtk.TargetEntry.new('text/plain', Gtk.TargetFlags(4), 129)
            textview.drag_dest_set(Gtk.DestDefaults.ALL, [enforce_target], Gdk.DragAction.COPY)
            textview.connect("drag-data-received", self.on_drag_data_received)
            #textview.drag_dest_set_target_list([enforce_target])
            window.add(textview)
            window.show_all()
    
        def on_drag_data_received(self, widget, drag_context, x,y, data,info, time):
            print (data.get_text())
    
    def main():
        app = GUI()
        Gtk.main()
            
    if __name__ == "__main__":
        sys.exit(main())