簡體   English   中英

wxPython ListCtrl復制到剪貼板

[英]wxPython ListCtrl copy to clipboard

我使用ListCtrl作為日志文件查看器,以便我可以隱藏普通用戶的“調試”類型的列。 我希望能夠像許多其他網格類型的程序一樣選擇多個單元格,然后右鍵單擊並說“復制”,然后將其粘貼到文本文檔,電子郵件等中。希望能夠選擇任何連續單元格的分組,而不是僅限於整行。

有什么內置的東西可以幫我嗎? 我怎么做到這一點? 我應該切換到虛擬或終極ListCtrl嗎? 也許我應該使用其他一些wxPython類?

一個工作的例子:

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表格)可能更合適。 這個想法仍然是相同的,只是收集列表項(或單元項)的部分是不同的。

暫無
暫無

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

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