wxPython listcrlt复制到剪贴板

2024-06-08 03:16:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用listcrl作为日志文件查看器,这样就可以对普通用户隐藏“调试”类型的列。我希望能够选择多个单元格,就像你可以在许多其他网格类型的程序,然后右键单击并说“复制”,然后能够粘贴到一个文本文档,电子邮件等。我希望能够选择任何分组的连续单元格,而不是仅限于整行。在

有没有什么内置的东西能帮我做到这一点?我该怎么做呢?我应该切换到虚拟的还是终极的listcrl?也许我应该用其他的wxPython类?在


Tags: 文件程序网格类型粘贴电子邮件wxpython文本文档
1条回答
网友
1楼 · 发布于 2024-06-08 03:16:23

一个有效的例子:

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-sheet)可能更适合。想法还是一样,只是收集列表项(或单元格项)的部分不同。在

相关问题 更多 >

    热门问题