Så här ser hela koden ut nu med samma problem som jag beskrivit tidigare.
Kod: Markera allt
from gi.repository import Gtk, Gdk
from gi.repository.GdkPixbuf import Pixbuf
(TARGET_ENTRY_TEXT, TARGET_ENTRY_PIXBUF) = range(2)
(COLUMN_TEXT, COLUMN_PIXBUF) = range(2)
DRAG_ACTION = Gdk.DragAction.COPY
class DragDropWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Drag and Drop Demo")
Gtk.Window.set_default_size(self,700,600)
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
self.add(vbox)
hbox = Gtk.Box(spacing=12)
vbox.pack_start(hbox, True, True, 0)
pixbufImg = Pixbuf.new_from_file_at_size("/home/jonas/test/test-project.svg", 128, -1)
pixbufImg2 = Pixbuf.new_from_file_at_size("/home/jonas/test/jag.jpg", 128, -1)
pixbufImg3 = Pixbuf.new_from_file_at_size("/home/jonas/test/Katt Avatar.jpg", 128, -1)
self.iconview = myIconView()
self.iconview.add_item("image1", pixbufImg)
self.iconview.add_item("image2", pixbufImg2)
self.iconview.add_item("image3", pixbufImg3)
self.iconview2 = myIconView()
hbox.pack_start(self.iconview, True, True, 0)
hbox.pack_start(self.iconview2, True, True, 0)
button_box = Gtk.Box(spacing=6)
vbox.pack_start(button_box, True, False, 0)
image_button = Gtk.RadioButton.new_with_label_from_widget(None, "Images")
image_button.connect("toggled", self.add_image_targets)
button_box.pack_start(image_button, True, False, 0)
text_button = Gtk.RadioButton.new_with_label_from_widget(image_button, "Text")
text_button.connect("toggled", self.add_text_targets)
button_box.pack_start(text_button, True, False, 0)
self.add_image_targets()
def add_image_targets(self, button=None):
targets = Gtk.TargetList.new([])
targets.add_image_targets(TARGET_ENTRY_PIXBUF, False)
self.iconview2.drag_dest_set_target_list(targets)
self.iconview.drag_source_set_target_list(targets)
def add_text_targets(self, button=None):
self.iconview2.drag_dest_set_target_list(None)
self.iconview.drag_source_set_target_list(None)
self.iconview2.drag_dest_add_text_targets()
self.iconview.drag_source_add_text_targets()
class myIconView(Gtk.IconView):
def __init__(self):
Gtk.IconView.__init__(self)
self.set_text_column(COLUMN_TEXT)
self.set_pixbuf_column(COLUMN_PIXBUF)
self.model = Gtk.ListStore(str, Pixbuf)
self.set_model(self.model)
self.drag_dest_set(Gtk.DestDefaults.ALL, [], DRAG_ACTION)
self.set_columns(1)
self.connect("drag-data-received", self.on_drag_data_received)
self.enable_model_drag_source(Gdk.ModifierType.BUTTON1_MASK, [], DRAG_ACTION)
self.connect("drag-data-get", self.on_drag_data_get)
def on_drag_data_get(self, widget, drag_context, data, info, time):
selected_path = self.get_selected_items()[0]
selected_iter = self.get_model().get_iter(selected_path)
text = self.get_model().get_value(selected_iter, COLUMN_TEXT)
pixbuf = self.get_model().get_value(selected_iter, COLUMN_PIXBUF)
print(text)
data.set_text(text, -1)
data.set_pixbuf(pixbuf)
def add_item(self, text, icon_name):
self.get_model().append([text, icon_name])
def on_drag_data_received(self, widget, drag_context, x, y, data, info, time):
model = widget.get_model()
pixbuf = data.get_pixbuf()
text = data.get_text()
print(text)
try:
path, position = widget.get_dest_item_at_pos(x, y)
myIter = model.get_iter(path)
except:
model.append([text, pixbuf])
position = None
if (position == Gtk.IconViewDropPosition.DROP_LEFT or position == Gtk.IconViewDropPosition.DROP_ABOVE):
model.insert_before(myIter, [data.get_text(), pixbuf])
elif (position == Gtk.IconViewDropPosition.DROP_RIGHT or position == Gtk.IconViewDropPosition.DROP_BELOW):
model.insert_after(myIter, [data.get_text(), pixbuf])
win = DragDropWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()