我在这里有一个Gtk.TreeView
。大多数但不是全部项目都应该可以拖放。在此示例中,第一项应该不能能够拖放,但应该可以选择。
我怎么能意识到这一点?也许我必须使用drag-begin
信号并在那里停止拖动。但是我不知道如何。
#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
class MainWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="TreeView Drag and Drop")
self.connect("delete-event", Gtk.main_quit)
self.set_default_size(400, 300)
# "model" with dummy data
self.store = Gtk.TreeStore(str)
self.store.append(None, ['do not drag this'])
self.store.append(None, ['drag this'])
self.view = Gtk.TreeView(model=self.store)
self.add(self.view)
# build columsn
colA = Gtk.TreeViewColumn('Col A', Gtk.CellRendererText(), text=0)
self.view.append_column(colA)
# DnD events
self.view.connect("drag-data-received", self.drag_data_received)
self.view.connect("drag-data-get", self.drag_data_get)
self.view.connect("drag-begin", self.drag_begin)
target_entry = Gtk.TargetEntry.new('text/plain', 2, 0)
self.view.enable_model_drag_source(
Gdk.ModifierType.BUTTON1_MASK,[target_entry],
Gdk.DragAction.DEFAULT|Gdk.DragAction.MOVE
)
self.view.enable_model_drag_dest(
[target_entry],
Gdk.DragAction.DEFAULT|Gdk.DragAction.MOVE
)
def drag_data_get (self, treeview, drag_context, data, info, time):
model, path = treeview.get_selection().get_selected_rows()
print('dd-get\tpath: {}'.format(path))
data.set_text(str(path[0]), -1)
def drag_data_received (self, treeview, drag_context, x,y, data,info, time):
print('dd-received')
store = treeview.get_model()
source_iter = store.get_iter(data.get_text())
dest_path, drop_pos = self.view.get_dest_row_at_pos(x, y)
print('path: {} pos: {}'.format(dest_path, drop_pos))
def drag_begin(self, widget, context):
print(widget)
print(context)
win = MainWindow()
win.show_all()
Gtk.main()