Python3 GTK3 TextView Drag-and-Drop Example

Just a small finger exercise …

#!/usr/bin/env python3

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
import os
import sys

class GUI:
    def __init__(self):
        self.window = Gtk.Window()
        self.window.connect('destroy', Gtk.main_quit)
        self.textview = Gtk.TextView()
        self.textview.connect("drag-data-received", self.on_drag)
        self.window.add(self.textview)
        self.window.show_all()
        self.buffer = self.textview.get_buffer()

    def on_drag(
        self,
        widget,
        drag_context,
        x,
        y,
        data,
        info,
        time
    ):
        text = data.get_text()
        print("DEBUG: text = %s" % text)
        self.buffer.set_text("")

def main():
    app = GUI()
    Gtk.main()

if __name__ == "__main__":
    sys.exit(main())
Imprint RSS