wxPython ObjectListView 捕获 Ctrl-C 快捷键 [英] wxPython ObjectListView Capture Ctrl-C shortcut

查看:97
本文介绍了wxPython ObjectListView 捕获 Ctrl-C 快捷键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 wxPython 2.9.5、ObjectListView 1.2、lxml 2.3 和 SQLAlchemy 0.8.2 用 Python 2.7.5 编写并使用 py2exe 编译成 exe 时遇到困难.

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.

我遇到的问题是在将程序编译为 exe 后,我无法再使用 Ctrl-C 从 ObjectListView 复制数据并将其粘贴到其他程序中,例如 Excel、记事本,甚至 Notepad++.无论选择复制/粘贴多少行,都会发生这种情况.粘贴到 Excel 时得到的结果是Microsoft Excel 无法粘贴数据".当我尝试记事本时,我得到了第一列和第一行的第一个字母(我认为它是第一行),但没有别的.

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.

这是我遇到问题的输出窗口之一的代码片段(总共有 2 个,但它们几乎是彼此的完美镜像).这是由主脚本导入,然后作为页面添加到 agw AUI Notebook.

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

我的主要问题是如何在使用 py2exe 编译后让键盘快捷键 Ctrl+C 正常工作?

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

在我收到Try PyInstaller"的回复之前,我每次都收到一条错误消息,但这是另一个可能的问题发布.

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.

提前致谢.

--编辑--

我认为这段代码将是我的解决方案,但我的最终结果是外语的混搭.

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

让它按预期工作,感谢 Mike Driscoll,谢谢!我确实改变了将数据放入剪贴板的方式,我认为它不是最有效的,但它有效.我还发现了为什么我在粘贴时会收到一堆乱七八糟的东西.代码如下,我使用多行 TextCtrl 临时存储数据并在完成后清除它.

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

我玩弄的部分是:

    self.dataObj.SetData

它实际上需要:

    self.dataObj.SetText

通过在每行的花絮之间使用 \t,我可以毫无问题地粘贴到 Excel 中!

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

非常感谢你迈克 D.

推荐答案

您可能需要明确使用剪贴板来完成这项工作.我不知道你为什么会,但这是我的第一个想法.以下是有关该主题的几个教程:

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:

我也会在 wxPython 用户列表/Google 群组上询问.许多开发人员都在那里闲逛,也许可以为您提供额外的指导:

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:

这篇关于wxPython ObjectListView 捕获 Ctrl-C 快捷键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆