wxPython ObjectListView Capture Ctrl-C shortcut

2019-08-17 16:29发布

问题:

I am having difficulty with my program written in Python 2.7.5 using wxPython 2.9.5, ObjectListView 1.2, lxml 2.3, and SQLAlchemy 0.8.2 and compiled into an exe with py2exe.

The problem I am running into is after compiling the program into an exe I am no longer able to use Ctrl-C to copy data from the ObjectListView and paste it into other programs such as Excel, Notepad, or even Notepad++. This happens regardless of how many rows are selected to be copy/pasted. The results I get when pasting into Excel is "Microsoft Excel cannot paste the data." and when I try notepad I get the very first letter from the first column and row (I think it is the first row) but nothing else.

Here is a code snippet from one of the output windows I am having the issues with (there are 2 total, but they are almost perfect mirrors of each other). This is imported by the main script and then added to an agw AUI Notebook as a page.

import wx
from ObjectListView import ObjectListView, ColumnDefn
from wx.lib.pubsub import pub
import ectworker as EW

TabLabel = 'Primary Output'
outputOne = []

class TabPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        headerFont = wx.Font(12, wx.MODERN, wx.SLANT, wx.NORMAL)
        title = wx.StaticText(self, -1, " Primary Output:")
        title.SetFont(headerFont)

        self.countDisplay = wx.StaticText(self, -1, '%s'%len(invOutput))
        self.countDisplay.SetFont(headerFont)

        self.dataOLV = ObjectListView(self, wx.ID_ANY,
            style=wx.LC_REPORT|wx.SUNKEN_BORDER)
        self.setColumns()
        self.dataOLV.SetEmptyListMsg('Please click the show button.\n\
            If nothing appears then there is nothing to display')
        self.dataOLV.cellEditMode = ObjectListView.CELLEDIT_NONE

        updateBtn = wx.Button(self, wx.ID_ANY, 'Show')
        updateBtn.Bind(wx.EVT_BUTTON, self.updateControl)

        filterBtn = wx.Button(self, label='Filter')
        filterBtn.Bind(wx.EVT_BUTTON, self.filterInfo)

        lookupBtn = wx.Button(self, wx.ID_ANY, 'Look Up')
        lookupBtn.Bind(wx.EVT_BUTTON, self.onLookUp)

        vbox = wx.BoxSizer(wx.VERTICAL)
        hbox0 = wx.BoxSizer(wx.HORIZONTAL)
        hbox0.Add(title, 0, wx.ALL, 5)
        hbox0.Add(self.countDisplay, 0, wx.ALL, 5)
        hbox1 = wx.BoxSizer(wx.HORIZONTAL)
        hbox1.Add(updateBtn, 0, wx.ALL, 5)
        hbox1.Add(filterBtn, 0, wx.ALL, 5)
        hbox1.Add(lookupBtn, 0, wx.ALL, 5)
        vbox.Add(hbox0, 0, wx.EXPAND, 5)
        vbox.Add(self.dataOLV, 1, wx.ALL|wx.EXPAND, 5)
        vbox.Add(hbox1, 0, wx.ALL|wx.CENTER, 5)
        self.SetSizer(vbox)

    def setColumns(self, data=None):
        self.idColumn = ColumnDefn('Dealer ID', 'left', -1, 'id')
        self.nameColumn = ColumnDefn('Dealer Name', 'left', -1, 'name')
        self.thirdColumn = ColumnDefn('Third', 'left', -1, 'third')
        self.fourthColumn = ColumnDefn('Fourth', 'left', -1, 'fourth')
        self.fifthColumn = ColumnDefn('Fifth', 'left', -1, 'fifth')
        self.sixthColumn = ColumnDefn('Sixth', 'right', -1, 'sixth')
        self.seventhColumn = ColumnDefn('Seventh', 'right', -1, 'seventh')
        self.olvColumns = [
            self.idColumn,
            self.nameColumn,
            self.thirdColumn,
            self.fourthColumn,
            self.fifthColumn,
            self.sixthColumn,
            self.seventhColumn]

        self.dataOLV.SetColumns(self.olvColumns)

    def updateControl(self, event):
        outputOne = EW.filterOne()
        self.updateOLV(outputOne)        

    def filterInfo(self, event):
        pub.sendMessage('dialog.filter', event=None)
        while EW.dataVars.theFilter == []:
            pub.sendMessage('dialog.filter', event=None)
        outputOne = EW.filterOne()
        self.updateOLV(outputOne)

    def updateOLV(self, object):
        self.dataOLV.SetObjects(object)
        self.countDisplay.SetLabel('%s'%len(object))

    def onLookUp(self, event):
      # This needs proper error handling
        selectedRow = self.dataOLV.GetSelectedObject()
        try:
            webbrowser.open('sc/%s' % selectedRow.id)
        except AttributeError:
            pass

if __name__ == '__main__':
    print('File file is not meant to be run as a stand alone application.')
    print('  Please try again by running the main program.')

My primary question is how can I get the keyboard shortcut Ctrl+C to properly function after compiling with py2exe?

Before I receive the responses of "Try PyInstaller", I have and I receive an error message each time, but that is for another possible question posting.

Thanks in advance.

--EDIT--

I thought this code would be my solution, however my final result is a jumbled mash-up of foreign languages.

def onCtrlC(self, event):
    selectedRows = self.dataOLV.GetSelectedObjects()
    self.dataObj = wx.TextDataObject()
    self.dataObj.SetData('%s'%str(selectedRows))
    if wx.TheClipboard.Open():
        wx.TheClipboard.SetData(self.dataObj)
        wx.TheClipboard.Flush()
    else:
        pub.sendMessage('dialog.error', message='Unable to open the clipboard',
                        caption='Error')

I'm making an educated guess that I am missing something.

--EDIT2--

Got it working as desired, thanks to Mike Driscoll, thank you! I did change how I was putting the data into the clipboard, I don't think it is the most efficient, but it works. I also discovered why I was receiving a jumbled mess when pasting. Code is below, I am using a multiline TextCtrl to temporarily store the data and clearing it when I am done.

self.hiddenTxt = wx.TextCtrl(self, style=wx.TE_MULTILINE)
self.hiddenTxt.Hide()

def onCtrlC(self, event):
    selectedRows = self.dataOLV.GetSelectedObjects()
    for x in selectedRows:
        self.hiddenTxt.AppendText('%s\t%s\n'%(x.id, x.name))
    self.dataObj = wx.TextDataObject()
    self.dataObj.SetText(self.hiddenTxt.GetValue())
    if wx.TheClipboard.Open():
        wx.TheClipboard.SetData(self.dataObj)
        wx.TheClipboard.Flush()
    else:
        pub.sendMessage('dialog.error', message='Unable to open the clipboard',
                        caption='Error')
    self.hiddenTxt.SetValue('')

The part I goofed on was:

    self.dataObj.SetData

It actually needed to be:

    self.dataObj.SetText

By using \t between the tidbits on each line I am able to paste into Excel with no issues at all!

Many, Many thanks to you Mike D.

回答1:

You may need to use the clipboard explicitly to make this work. I don't know why you would, but that's my first thought. Here are a couple of tutorials on the subject:

  • http://www.blog.pythonlibrary.org/2012/05/25/wxpython-how-to-use-the-clipboard/
  • http://wiki.wxpython.org/ClipBoard

I would also ask on the wxPython users list / Google group. A lot of developers hang out there and may be able to give you additional pointers:

  • https://groups.google.com/forum/#!forum/wxpython-users