Copy and paste rows in wxpython using a virtual ListCtrl

不羁岁月 提交于 2019-12-11 05:15:46

问题


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!


回答1:


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/




回答2:


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))


来源:https://stackoverflow.com/questions/12780131/copy-and-paste-rows-in-wxpython-using-a-virtual-listctrl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!