gpt4 book ai didi

python - wxPython - DropSource.SetData() 适用于字符串,但不适用于列表

转载 作者:行者123 更新时间:2023-12-01 08:19:55 25 4
gpt4 key购买 nike

我想将一个列表项拖放到另一个列表中。两个列表都有两列。我通过将值从第一列拖放到另一个列表来实现它。但是将机器人值从第 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.TextDropTargetwx.TextDataObject 一起使用,并且您传递的显然是一个列表。< br/>您需要创建自定义数据对象,然后在拖动时序列化列表并在放置时反序列化。

要序列化/反序列化列表,您可以使用 picklemarshal,我相信您可以使用 json以及,但我并没有真正研究过这一点。

列表拖放的编码可能会变得相当复杂,因此我将这些代码放在一起,以便您直观地了解如何实现它。

代码已注释,因此希望您能够看到什么是什么。
它比实际情况稍微复杂一些,因为我允许从列数不等的列表中拖放。
另请注意,您可以拖放到同一列表中。

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-dropoperation 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 theflags, data may be moved and not only copied as is the case for thedefault wx.Drag_CopyOnly . If wx.Drag_DefaultMove is specified (whichincludes the previous flag), moving is not only possible but becomesthe default operation. Return type: wx.DragResult Returns: Theoperation requested by the user, may be wx.DragCopy , wx.DragMove ,wx.DragLink , wx.DragCancel or wx.DragNone if an error occurred.

Initial View

Drag 2 items from left panel to right panel (inserting)

Drag 2 items from right panel to left panel (appending)

关于python - wxPython - DropSource.SetData() 适用于字符串,但不适用于列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54711220/

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