TypeError:'in <string>'需要字符串作为左操作数,而不是int with ObjectListView wxPython </string>

时间:2015-03-27 14:18:42

标签: python wxpython objectlistview-python

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

只需按任意键,例如&#39; 3&#39;&#39; z&#39; &#39; X&#39;等等......每次都会出现红色错误信息。

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

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

evt.GetUnicodeKey应该按照Phoenix doc返回一个字符串: 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