一个可工作的示例:
import wx
class Frame(wx.Frame):
def __init__(self):
super(Frame, self).__init__(None, -1, "List copy test", size=(400, 500))
panel = wx.Panel(self, -1)
self.listCtrl = wx.ListCtrl(panel, -1, size=(200, 400), style=wx.LC_REPORT)
self.listCtrl.InsertColumn(0, "Column 1", width=180)
for i in xrange(10):
self.listCtrl.InsertStringItem(i, "Item %d" % i)
self.listCtrl.Bind(wx.EVT_RIGHT_UP, self.ShowPopup)
def ShowPopup(self, event):
menu = wx.Menu()
menu.Append(1, "Copy selected items")
menu.Bind(wx.EVT_MENU, self.CopyItems, id=1)
self.PopupMenu(menu)
def CopyItems(self, event):
selectedItems = []
for i in xrange(self.listCtrl.GetItemCount()):
if self.listCtrl.IsSelected(i):
selectedItems.append(self.listCtrl.GetItemText(i))
clipdata = wx.TextDataObject()
clipdata.SetText("\n".join(selectedItems))
wx.TheClipboard.Open()
wx.TheClipboard.SetData(clipdata)
wx.TheClipboard.Close()
print "Items are on the clipboard"
app = wx.App(redirect=False)
frame = Frame()
frame.Show()
app.MainLoop()
您提到了列表控件,但如果您想选择多个单元格,则类似于Excel表格的网格控件可能更合适。 思路仍然相同,只是收集列表项(或单元格项)的部分不同。