gpt4 book ai didi

python - 取消对 Gtk.TreeView 中某些特定项目的拖放

转载 作者:太空狗 更新时间:2023-10-30 02:52:48 29 4
gpt4 key购买 nike

我这里有一个 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()

最佳答案

你必须重载 Gtk.TreeDragSource.do_row_draggable() .

class MyTreeStore (Gtk.TreeStore):
# ...
def do_row_draggable(self, path):
# do your decision here

return True # draggable
return False # NOT draggable

关于python - 取消对 Gtk.TreeView 中某些特定项目的拖放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51974845/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com