【问题标题】:TypeError: 'in <string>' requires string as left operand, not int with ObjectListView wxPythonTypeError: 'in <string>' 需要字符串作为左操作数,而不是 int 与 ObjectListView wxPython
【发布时间】:2015-03-27 14:18:42
【问题描述】:

在 Python 2.7 中使用 ObjectListView 时 - 按键盘上的任何字母数字键,我在 IDE 中收到以下消息错误(使用 PyCharm):

C:\Users\dylan_cissou\AppData\Local\Continuum\Anaconda\python.exe C:/Users/dylan_cissou/PycharmProjects/SPA/example.py
Traceback (most recent call last):
  File "build\bdist.win-amd64\egg\ObjectListView\ObjectListView.py", line 1410, in _HandleChar
  File "build\bdist.win-amd64\egg\ObjectListView\ObjectListView.py", line 1457, in _HandleTypingEvent
TypeError: 'in <string>' requires string as left operand, not int

我应该怎么做才能禁用此消息?我试图找到这两个事件的位置,以便我可以覆盖它们,但我找不到任何事件。

代码示例如下:

import wx
from ObjectListView import ObjectListView, ColumnDefn

########################################################################
class Book(object):
    """
    Model of the Book object

    Contains the following attributes:
    'ISBN', 'Author', 'Manufacturer', 'Title'
    """
    #----------------------------------------------------------------------
    def __init__(self, title, author, isbn, mfg):
        self.isbn = isbn
        self.author = author
        self.mfg = mfg
        self.title = title


########################################################################
class MainPanel(wx.Panel):
    #----------------------------------------------------------------------
    def __init__(self, parent):
        wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
        self.products = [Book("wxPython in Action", "Robin Dunn",
                              "1932394621", "Manning"),
                         Book("Hello World", "Warren and Carter Sande",
                              "1933988495", "Manning")
                         ]

        self.dataOlv = ObjectListView(self, wx.ID_ANY, style=wx.LC_REPORT|wx.SUNKEN_BORDER)
        self.setBooks()

        # Allow the cell values to be edited when double-clicked
        self.dataOlv.cellEditMode = ObjectListView.CELLEDIT_SINGLECLICK

        # create an update button
        updateBtn = wx.Button(self, wx.ID_ANY, "Update OLV")
        updateBtn.Bind(wx.EVT_BUTTON, self.updateControl)

        # Create some sizers
        mainSizer = wx.BoxSizer(wx.VERTICAL)        

        mainSizer.Add(self.dataOlv, 1, wx.ALL|wx.EXPAND, 5)
        mainSizer.Add(updateBtn, 0, wx.ALL|wx.CENTER, 5)
        self.SetSizer(mainSizer)

    #----------------------------------------------------------------------
    def updateControl(self, event):
        """

        """
        print "updating..."
        product_dict = [{"title":"Core Python Programming", "author":"Wesley Chun",
                         "isbn":"0132269937", "mfg":"Prentice Hall"},
                        {"title":"Python Programming for the Absolute Beginner",
                         "author":"Michael Dawson", "isbn":"1598631128",
                         "mfg":"Course Technology"},
                        {"title":"Learning Python", "author":"Mark Lutz",
                         "isbn":"0596513984", "mfg":"O'Reilly"}
                        ]
        data = self.products + product_dict
        self.dataOlv.SetObjects(data)

    #----------------------------------------------------------------------
    def setBooks(self, data=None):
        self.dataOlv.SetColumns([
            ColumnDefn("Title", "left", 220, "title"),
            ColumnDefn("Author", "left", 200, "author"),
            ColumnDefn("ISBN", "right", 100, "isbn"),            
            ColumnDefn("Mfg", "left", 180, "mfg")
        ])

        self.dataOlv.SetObjects(self.products)

########################################################################
class MainFrame(wx.Frame):
    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, parent=None, id=wx.ID_ANY, 
                          title="ObjectListView Demo", size=(800,600))
        panel = MainPanel(self)

########################################################################
class GenApp(wx.App):

    #----------------------------------------------------------------------
    def __init__(self, redirect=False, filename=None):
        wx.App.__init__(self, redirect, filename)

    #----------------------------------------------------------------------
    def OnInit(self):
        # create frame here
        frame = MainFrame()
        frame.Show()
        return True

#----------------------------------------------------------------------
def main():
    """
    Run the demo
    """
    app = GenApp()
    app.MainLoop()

if __name__ == "__main__":
    main()

只需按任意键,例如“3”、“z”、“x”等...每次都会显示红色错误消息。

感谢您的帮助。

【问题讨论】:

  • 我刚刚在 Xubuntu 14.04、wxPython 2.8.12 和 Python 2.7 上使用您的代码进行了尝试。我无法重现该问题。我很确定我最初编写了该代码,并且它在 Windows 7 上也运行良好。您使用的是什么操作系统、wxPython 和 OLV 版本?
  • 感谢耐克的帮助。我在 Windows 7 和 wxPython 3.0 中使用 objectlistview-1.3.1。

标签: python wxpython objectlistview-python


【解决方案1】:

嗯,这似乎是 wxPython 的一个问题,我在 2.8.12 Unicode 版本、3.0.2 和 Phoenix 上使用 OLV 1.3.2 的 Python 2.7 上看到了同样的问题

evt.GetUnicodeKey 应该根据 Phoenix 文档返回一个字符串: http://wxpython.org/Phoenix/docs/html/KeyEvent.html?highlight=getkeycode#KeyEvent.GetUnicodeKey

根据 Robin Dunn 的说法,这是不正确的,它应该返回一个 int。

我在 wxPython-dev 上发布了一个关于此的问题。

我将在olv._HandleTypingEvent中进行修改,如下::

if evt.GetUnicodeKey() == 0:
    uniChar = chr(evt.GetKeyCode())
else:
    uniChar = evt.GetUnicodeKey()
if uniChar not in string.printable:
    return False

到:

if evt.GetUnicodeKey() == 0:
    uniChar = chr(evt.GetKeyCode())
else:
    uniChar = chr(evt.GetUnicodeKey())
if uniChar not in string.printable:
    return False

【讨论】:

  • 非常感谢 - 实际上这是我想尝试的第一件事,但我找不到 olv._HandleTypingEvent。这可能是一个愚蠢的问题,但我在哪里可以找到这部分代码?
  • ObjectListView 实在是太长了,所以我总是用 olv 作为缩写。顺便说一句,您得到的异常也在最后一行显示。
  • 问题是我找不到 olv 的源代码来修改 _Handle 事件代码 - 在我看来 olv 库是在 objectlistview-1.3.1-py2.7.egg 中编译的
  • .egg 是一个包含源代码的压缩文件。如果您升级到 1.3.2,则代码位于 ObjectListView 包文件夹中。
  • 仅供参考,evt.GetUnicodeKey 可能不是最终解决方案,仍在研究中。
猜你喜欢
  • 1970-01-01
  • 2023-04-08
  • 1970-01-01
  • 1970-01-01
  • 2021-08-22
  • 1970-01-01
  • 2021-07-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多