简体   繁体   English

wxPython ListCtrl复制到剪贴板

[英]wxPython ListCtrl copy to clipboard

I am using a ListCtrl as a log file viewer so that I can hide "debug" type of columns from the average user. 我使用ListCtrl作为日志文件查看器,以便我可以隐藏普通用户的“调试”类型的列。 I would like to be able to select multiple cells just like you can do in many other grid-type programs, and then right click and say "Copy", and then be able to paste that into a text doc, email, etc. I would like to be able to select any grouping of contiguous cells, rather than be limited to whole rows only. 我希望能够像许多其他网格类型的程序一样选择多个单元格,然后右键单击并说“复制”,然后将其粘贴到文本文档,电子邮件等中。希望能够选择任何连续单元格的分组,而不是仅限于整行。

Is there anything built-in that does this for me? 有什么内置的东西可以帮我吗? How would I accomplish that? 我怎么做到这一点? Should I switch to a virtual or ultimate ListCtrl? 我应该切换到虚拟或终极ListCtrl吗? Maybe I should be using some other wxPython class? 也许我应该使用其他一些wxPython类?

A working example: 一个工作的例子:

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

You mentioned a list control, but if you want to select multiple cells, perhaps a grid control (excel sheet like) might be more suitable. 你提到了一个列表控件,但如果你想选择多个单元格,也许网格控件(excel表格)可能更合适。 The idea is still the same only the part where the list items (or cell items) are collected is different. 这个想法仍然是相同的,只是收集列表项(或单元项)的部分是不同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM