Question

I'm using a virtual ListCtrl in wxpython. I am trying to select several rows from the list and then copy / paste the row value to a text file, or possibly spreadsheet. How would I copy the selected rows to clipboard (using CTRL-C)? Which event should I bind? Thanks!

Was it helpful?

Solution

Looking at the wxPython demo for the list control, I think you'd have to do something like the following:

index = self.list.GetFirstSelected()
value = "      %s: %s\n" % (self.list.GetItemText(index), self.getColumnText(index, 1)))

You would need to use an AcceleratorTable if you want to use CTRL-C, which means that you'd bind to EVT_MENU and put the code I mentioned in that handler. Here's a tutorial on Accerators in wx: http://www.blog.pythonlibrary.org/2010/12/02/wxpython-keyboard-shortcuts-accelerators/

On the other hand, I almost always use ObjectListView instead of ListCtrl as it gives me an object model of each row which I find a lot easier to access than using row and column indexes. It takes a slightly different approach and mindset, but I think it's worth it: http://www.blog.pythonlibrary.org/2009/12/23/wxpython-using-objectlistview-instead-of-a-listctrl/

OTHER TIPS

Mike's link on accelerators was really helpful. Along with this, I used pyperclip.copy() to complete my copy operation. With this, selected contents are copied to clipboard; and it can be pasted to any of the files.

Hope it helps someone..

import pyperclip 

def onKeyCombo(self, event):

    listSelectedLines =[]
    index = self.list.GetFirstSelected()  

    while index is not -1:
        listSelectedLines.append(self.list.GetItem(index, 1).GetText())
        index = self.list.GetNextSelected(index)             

    pyperclip.copy(''.join(listSelectedLines))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top