Portál AbcLinuxu, 9. května 2025 19:45

Dotaz: Python GTK+

21.7.2015 16:34 Ondřej Kolín
Python GTK+
Přečteno: 373×
Odpovědět | Admin

Zdravím, nejde mi rozchodit v Python 2.7.* a GTK+ (gi.repository) DnD mezi dvěma iconview, netuší někdo, kde by mohl být problém?



#!/usr/bin/env python
# -*- coding: utf-8 -*-
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

icons = ["gtk-cut", "gtk-paste", "gtk-copy"]

win = Gtk.Window()
content = Gtk.VBox()
#source view
liststore = Gtk.ListStore(Pixbuf, str)
sourceview = Gtk.IconView.new()
sourceview.set_model(liststore)
sourceview.set_pixbuf_column(0)
sourceview.set_text_column(1)

sourceview.enable_model_drag_source(Gdk.ModifierType.BUTTON1_MASK, [],
DRAG_ACTION)
def on_drag_data_get( widget, drag_context, data, info, time):
selected_path = self.get_selected_items()[0]
selected_iter = self.get_model().get_iter(selected_path)
print widget.get_model().get_iter(selected_iter, 1)
sourceview.connect("drag-data-get", on_drag_data_get)

for icon in icons:
pixbuf = Gtk.IconTheme.get_default().load_icon(icon, 64, 0)
liststore.append([pixbuf, "Label"])

content.add(sourceview)
#target view
targetstore = Gtk.ListStore(Pixbuf, str)
targetstore.append([Gtk.IconTheme.get_default().load_icon("gtk-save", 64,0), "X"])
targetview = Gtk.IconView.new()
targetview.set_model(targetstore)
targetview.set_pixbuf_column(0)
targetview.set_text_column(1)
targetview.drag_dest_set(Gtk.DestDefaults.ALL, [], DRAG_ACTION)


def on_drag_data_received(self, widget, drag_context, x,y, data,info, time):
text = data.get_text()
print("Received text: %s" % text)
targetview.connect("drag-data-received", on_drag_data_received)

content.add(targetview)

win.add(content)
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

Nástroje: Začni sledovat (1) ?Zašle upozornění na váš email při vložení nového komentáře.

Odpovědi

21.7.2015 16:35 Ondřej Kolín
Rozbalit Rozbalit vše Re: Python GTK+
Odpovědět | | Sbalit | Link | Blokovat | Admin
Nějak si nekamarádím se zdejším formátováním (byl jsem línej cheknout to jinde než v tom WISIWYG editoru, omlouvám se.

	

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    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
     
    icons = ["gtk-cut", "gtk-paste", "gtk-copy"]
     
    win = Gtk.Window()
    content = Gtk.VBox()
    #source view
    liststore = Gtk.ListStore(Pixbuf, str)
    sourceview = Gtk.IconView.new()
    sourceview.set_model(liststore)
    sourceview.set_pixbuf_column(0)
    sourceview.set_text_column(1)
     
    sourceview.enable_model_drag_source(Gdk.ModifierType.BUTTON1_MASK, [],
                DRAG_ACTION)
    def on_drag_data_get( widget, drag_context, data, info, time):
        selected_path = self.get_selected_items()[0]
        selected_iter = self.get_model().get_iter(selected_path)
        print widget.get_model().get_iter(selected_iter, 1)
    sourceview.connect("drag-data-get", on_drag_data_get)
     
    for icon in icons:
        pixbuf = Gtk.IconTheme.get_default().load_icon(icon, 64, 0)
        liststore.append([pixbuf, "Label"])
     
    content.add(sourceview)
    #target view
    targetstore = Gtk.ListStore(Pixbuf, str)
    targetstore.append([Gtk.IconTheme.get_default().load_icon("gtk-save", 64,0), "X"])
    targetview = Gtk.IconView.new()
    targetview.set_model(targetstore)
    targetview.set_pixbuf_column(0)
    targetview.set_text_column(1)
    targetview.drag_dest_set(Gtk.DestDefaults.ALL, [], DRAG_ACTION)
     
     
    def on_drag_data_received(self, widget, drag_context, x,y, data,info, time):
        text = data.get_text()
        print("Received text: %s" % text)
    targetview.connect("drag-data-received", on_drag_data_received)
     
    content.add(targetview)
     
    win.add(content)
    win.connect("delete-event", Gtk.main_quit)
    win.show_all()
    Gtk.main()


21.7.2015 17:46 chrono
Rozbalit Rozbalit vše Re: Python GTK+
Odpovědět | | Sbalit | Link | Blokovat | Admin
Problém je, že je parameter targets pri enable_model_drag_source a drag_dest_set prázdny. Je možné to tam dať priamo pri volaní tej funkcie, prípadne sa to môže nastaviť neskôr, pomocou drag_source_set_target_list a drag_dest_set_target_list (prípadne sa môžu použiť pomocné funkcie, ktoré umožňujú jednoduché pridanie niektorých formátov).
21.7.2015 20:23 KOLEGA
Rozbalit Rozbalit vše Re: Python GTK+
Díky za odpověď, mohl by jsi mi to hodit do kódu? Omlouvám se, že jsem tak neschopnej, ale jsem úplně vygumovanej ... Mě jde jenom o to abych při drag n drop něco vypsal ... Snad se dostanu pak zase dál ... Ještě jednou fakt díky
21.7.2015 21:34 chrono
Rozbalit Rozbalit vše Re: Python GTK+
V tom originálnom kóde ale všetko potrebné je (je tam aj to vypísanie niečoho): 19. Drag and Drop
21.7.2015 22:14 KOLEGA | skóre: 17 | blog: odpocinuti_vecne
Rozbalit Rozbalit vše Re: Python GTK+
Díky za link, podle něj jsem to nerozchodil ... Jde o to, že autor tam používá label, kterej se chová trochu jinak než itemview ...

Zkoušel jsem přepisovat jeden cčkovej kód, kde to chodilo, ale ten jsem taky nerozchodil ...

Nicméně stejně díky, asi to nechám odležet, až se něco doučím, tak se k tomu vrátím ...

tady je ten odkaz na to cčko, což je skoro to přesně co chci ...
21.7.2015 22:14 KOLEGA | skóre: 17 | blog: odpocinuti_vecne
Rozbalit Rozbalit vše Re: Python GTK+
http://pastebin.com/dxyBaqhU

Založit nové vláknoNahoru

Tiskni Sdílej: Linkuj Jaggni to Vybrali.sme.sk Google Del.icio.us Facebook

ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.