Я хочу перетащить элемент списка в другой список. Оба списка имеют две колонки. Я заставил его работать с перетаскиванием значения из первого столбца в другой список. Но перетащить значения бота из столбцов 0 и 1 в другой список не получится.
Как использовать DropSource.SetData() со списком?
Вот часть моего кода:
def OnDragInit(self, event):
#text = self.lst1.GetItemText(event.GetIndex(),0)
#tobj = wx.TextDataObject(text) #Doesnt work with a list
# With the above two lines everything is working fine!
# Error here
text = []
text.append(self.lst1.GetItemText(event.GetIndex(),0))
text.append(self.lst1.GetItemText(event.GetIndex(),1))
src = wx.DropSource(self.lst1)
src.SetData(text)
src.DoDragDrop(True)
self.lst1.DeleteItem(event.GetIndex())
Вот сообщение об ошибке:
TypeError: DropSource.SetData(): argument 1 has unexpected type 'list'






Я подозреваю, что вы используете wx.TextDropTarget с wx.TextDataObject, и то, что вы передаете, явно является list.
Вам нужно будет создать собственный объект данных, затем сериализовать list при перетаскивании и десериализовать его при перетаскивании.
Чтобы сериализовать/десериализовать список, вы можете использовать pickle или marshal, и я склоняюсь к мысли, что вы также можете использовать json, но я действительно не рассматривал это.
Код для списка drag & drop может быть довольно сложным, поэтому я собрал этот код, чтобы вы могли представить, как вы можете его реализовать.
Код закомментирован, так что, надеюсь, вы сможете увидеть, что к чему.
Это немного сложнее, чем должно быть, так как я разрешил перетаскивание из/в списки с неравным количеством столбцов.
Также обратите внимание, что вы можете перетаскивать в тот же список.
import wx
import pickle
#import marshal
class MyTarget(wx.PyDropTarget):
def __init__(self, object):
wx.DropTarget.__init__(self)
self.object = object
# specify the type of data to accept
self.data = wx.CustomDataObject("ListCtrlItems")
self.SetDataObject(self.data)
# Called when OnDrop returns True.
def OnData(self, x, y, opt):
# Find insertion point in the target.
index, flags = self.object.HitTest((x, y))
if self.GetData():
# unpickle data
listdata = self.data.GetData()
dropped_list = pickle.loads(listdata)
#dropped_list = marshal.loads(listdata)
if index == -1: # if not inserting, set index to the end of the listctrl
index = self.object.GetItemCount()
#Insert at drop point
for row in dropped_list:
self.object.InsertItem(index, row[0]) #Insert item
cols = self.object.GetColumnCount()
for pos in range(1,cols):
try:
self.object.SetItem(index, pos, row[pos]) #Add extra columns data
except Exception as e: # run out of columns in target
pass
index +=1
return True
class Mywin(wx.Frame):
def __init__(self,parent,title):
wx.Frame.__init__(self, parent, wx.ID_ANY, title,size= (600,-1))
panel = wx.Panel(self)
box = wx.BoxSizer(wx.HORIZONTAL)
self.listCtrl1 = wx.ListCtrl(panel, -1, style = wx.LC_REPORT|wx.LC_HRULES)
self.listCtrl1.InsertColumn(0, "Item0")
self.listCtrl1.SetColumnWidth(0,100)
self.listCtrl1.InsertColumn(1, "Item1")
self.listCtrl1.SetColumnWidth(1,100)
self.listCtrl2 = wx.ListCtrl(panel, -1, style = wx.LC_REPORT|wx.LC_VRULES|wx.LC_HRULES)
self.listCtrl2.InsertColumn(0, "Item0")
self.listCtrl2.SetColumnWidth(0,100)
self.listCtrl2.InsertColumn(1, "Item1")
self.listCtrl2.SetColumnWidth(0,100)
self.listCtrl2.InsertColumn(2, "Item2")
self.delete = wx.CheckBox(panel, wx.ID_ANY, "Delete on move")
self.delete.SetToolTip("Delete original item when dragged & dropped")
self.delete.SetValue(True)
#load sample data
data = [["abc",1],["def",2],["ghi",3]]
for i in data:
self.listCtrl1.Append((i))
data = [["ABC",1,"first"],["DEF",2,"second"],["GHI",3,"third"]]
for i in data:
self.listCtrl2.Append((i))
#Target Left
tl = MyTarget(self.listCtrl1)
self.listCtrl1.SetDropTarget(tl)
#Target Right
tr = MyTarget(self.listCtrl2)
self.listCtrl2.SetDropTarget(tr)
self.listCtrl1.Bind(wx.EVT_LIST_BEGIN_DRAG, self.OnDrag)
self.listCtrl2.Bind(wx.EVT_LIST_BEGIN_DRAG, self.OnDrag)
box.Add(self.listCtrl1, 0, wx.EXPAND)
box.Add(self.listCtrl2, 0, wx.EXPAND)
box.Add(self.delete, 0, wx.ALIGN_TOP)
panel.SetSizer(box)
panel.Fit()
self.Centre()
self.Show(True)
def OnDrag(self, event):
#create a data object for drag-and-drop
object = event.GetEventObject() # listCtrl1 or listCtrl2
list_data = []
idx = -1
while True: # find all the selected items and put them in a list
idx = object.GetNextSelected(idx)
if idx == -1:
break
item_data = []
for item in range(object.GetColumnCount()): # get data from all columns
item_data.append(object.GetItem(idx, item).GetText())
list_data.append(item_data)
# Pickle the items list.
pickle_data = pickle.dumps(list_data)
#pickle_data = marshal.dumps(list_data)
# create custom data object
cdataobj = wx.CustomDataObject("ListCtrlItems")
cdataobj.SetData(pickle_data)
# Now make a data object for the item list.
data = wx.DataObjectComposite()
data.Add(cdataobj)
# Create drop source and begin drag-and-drop.
dropSource = wx.DropSource(object)
dropSource.SetData(data)
result = dropSource.DoDragDrop(True)
# delete dropped items from source list
if self.delete.GetValue(): # Is delete checkbox ticked
if result == wx.DragCopy: # Was the drag and drop successful
while True:
#For this small sample always start at the beginning (-1)
idx = object.GetNextSelected(-1)
if idx == -1: #No more selected items
break
object.DeleteItem(idx)
demo = wx.App()
Mywin(None,'Drag & Drop ListCtrl Demo')
demo.MainLoop()
В ответ на ваш комментарий о результате, полученном от DoDragDrop:
The DragResult enumeration provides the following values:
Description Value
DragError Error prevented the D&D operation from completing.
DragNone Drag target didn’t accept the data.
DragCopy The data was successfully copied.
DragMove The data was successfully moved (MSW only).
DragLink Operation is a drag-link.
DragCancel The operation was cancelled by user (not an error).DoDragDrop(self, flags=Drag_CopyOnly) Starts the drag-and-drop operation which will terminate when the user releases the mouse.
Call this in response to a mouse button press, for example.
Parameters: flags (int) – If wx.Drag_AllowMove is included in the flags, data may be moved and not only copied as is the case for the default wx.Drag_CopyOnly . If wx.Drag_DefaultMove is specified (which includes the previous flag), moving is not only possible but becomes the default operation. Return type: wx.DragResult Returns: The operation requested by the user, may be wx.DragCopy , wx.DragMove , wx.DragLink , wx.DragCancel or wx.DragNone if an error occurred.
Единственное, что не понятно: почему результат == wx.DragCopy не работает. wx.DragCopy Возвращает "2", если я печатаю его. результат 1 для истины.
@ mk1337 Смотрите мой исправленный ответ. True (1) был отправлен как флаг, result, возвращающийся из подпрограммы, — это совсем другое. Попробуйте перетащить элемент списка куда-нибудь, кроме одной из двух панелей, в зависимости от того, куда вы его перетащите, вы должны получить результат либо 1, либо 5, то есть DragNone или DragCancel.
Я понимаю, что вы говорите, и я также понимаю, что делает wx.DragCopy. Но также, если я перетащу что-нибудь еще, кроме двух панелей, он вернет «2». Я тестировал это с print(wx.DragCopy)
Большое тебе спасибо! Комментарии очень помогли мне разобраться во всем.