WxPython - 编写自动完成功能

时间:2015-07-06 10:22:52

标签: python wxpython

我正在使用wxPython(Phoenix)。 我根据这些guidelines编写了一个带有自定义自动填充程序的小应用程序,但它失败并出现以下错误:

Traceback (most recent call last):
  File "try2.py", line 33, in <module>
    frame = TextFrame()
  File "try2.py", line 26, in __init__
    basicText.AutoComplete(MyTextCompleter)
TypeError: TextEntry.AutoComplete(): arguments did not match any overloaded call:
  overload 1: argument 1 has unexpected type 'sip.wrappertype'
  overload 2: argument 1 has unexpected type 'sip.wrappertype' 

这是代码:

import wx
class MyTextCompleter(wx.TextCompleterSimple):

    def __init__(self):
        wx.TextCompleterSimple.__init__(self)

    def GetCompletions(self, prefix, res):
        if prefix == "a":
            res.append("This order is")
            res.append("very important")

        elif firstWord == "b":
            res.append("z - It's not in")
            res.append("a - lexicographic order")

        else:
            res.append("bye")

class TextFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Text Entry Example', size=(300, 100))
        panel = wx.Panel(self, -1) 
        basicLabel = wx.StaticText(panel, -1, "Basic Control:")
        basicText = wx.TextCtrl(panel, -1, "I've entered some text!", size=(175, -1))
        basicText.SetInsertionPoint(0)
        basicText.AutoComplete(MyTextCompleter)

        sizer = wx.FlexGridSizer(cols=2, hgap=6, vgap=6)
        sizer.AddMany([basicLabel, basicText])
        panel.SetSizer(sizer)

app = wx.PySimpleApp()
frame = TextFrame()
frame.Show()
app.MainLoop()

当评论出basicText.AutoComplete(MyTextCompleter)它成功运行时(没有自动完成)

2 个答案:

答案 0 :(得分:2)

我应该更多地警告你:wxPython Phoenix是wxPython的未来(因为,与经典相比,它也支持Python 3)。也就是说,这并不意味着一切都美好而有光泽。我个人的建议是继续使用经典(换句话说:现在在经典中有效,很可能在凤凰城也有效)。在凤凰城,你会经常偶然发现这样的错误。

幸运的是,在这种特殊情况下,已经有其他事情要做了:

<wx.TextCtrl>.AutoComplete(…)确实接受字符串列表。这已经适用于2.9.0 / classic。请参阅documentation for wx.TextEntry/AutoComplete

答案 1 :(得分:0)

您需要将MyTextCompleter的实例(而不是类本身)传递给wx.TextCtrl.AutoComplete()。改变这个:

        basicText.AutoComplete(MyTextCompleter)

        basicText.AutoComplete(MyTextCompleter())