簡體   English   中英

使用虛擬ListCtrl在wxpython中復制和粘貼行

[英]Copy and paste rows in wxpython using a virtual ListCtrl

我在wxpython中使用虛擬ListCtrl。 我試圖從列表中選擇幾行,然后將行值復制/粘貼到文本文件,或可能是電子表格。 如何將選定的行復制到剪貼板(使用CTRL-C)? 我應該綁定哪個事件? 謝謝!

查看列表控件的wxPython演示,我認為您必須執行以下操作:

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

如果要使用CTRL-C,則需要使用AcceleratorTable,這意味着您將綁定到EVT_MENU並將我提到的代碼放在該處理程序中。 這是關於wx中的Accerators的教程: http//www.blog.pythonlibrary.org/2010/12/02/wxpython-keyboard-shortcuts-accelerators/

另一方面,我幾乎總是使用ObjectListView而不是ListCtrl,因為它給我一個每行的對象模型,我發現它比使用行和列索引更容易訪問。 它需要稍微不同的方法和思維方式,但我認為這是值得的: http//www.blog.pythonlibrary.org/2009/12/23/wxpython-using-objectlistview-instead-of-a-listctrl/

Mike關於加速器的鏈接非常有用。 除此之外,我使用pyperclip.copy()來完成我的復制操作。 這樣,選定的內容被復制到剪貼板; 它可以粘貼到任何文件。

希望它可以幫助某人..

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM