wxpython comboBox 自动完成提示功能

摘要

最近在使用python做一个桌面应用,使用到了ComboBox这个控件,但是对于这个控件的想法是能够实现类似于百度搜索框的功能,输入相应的搜索内容,能够显示下拉列表,下拉列表中显示相关的提示信息。google了半天有的功能只有自动补充完成,并不能够达到显示下拉列表的形式,一旦遇到多个联想词的时候就会有些问题。所以自己研究了下,在其原先的基础上稍微修改了下代码实现了这个功能。下面来看看代码吧

正文

# -*- coding: utf-8 -*-
import wx

class PromptingComboBox(wx.ComboBox) :
    def __init__(self, parent, value, choices=[], style=0, **par):
        wx.ComboBox.__init__(self, parent, wx.ID_ANY, value, style=style|wx.CB_DROPDOWN, choices=choices, **par)
        self.choices = choices
        #分别绑定多个事件,文本内容变化,字符输入
        self.Bind(wx.EVT_TEXT, self.EvtText)
        self.Bind(wx.EVT_CHAR, self.EvtChar)
        self.Bind(wx.EVT_COMBOBOX, self.EvtCombobox) 
        self.ignoreEvtText = False

    def EvtCombobox(self, event):
        self.ignoreEvtText = True
        event.Skip()

    def EvtChar(self, event):
        #这里需要注意一点事,回车键如果不过滤掉的话,EvtText会类似于进入死循环,这里还不太清楚到底是为什么
        if event.GetKeyCode() == 8:
            self.ignoreEvtText = True
        event.Skip()

    def EvtText(self, event):

        currentText = event.GetString()
        #这里先判断内容是否为空,如果为空的话,需要让下拉菜单隐藏起来
        if currentText=='':
            self.SetItems(self.choices)
            self.Dismiss()
        if self.ignoreEvtText:
            self.ignoreEvtText = False
            return

        currentText = event.GetString()
        found = False
        choiceTemp = []
        for choice in self.choices :
            if choice.startswith(currentText):
                self.ignoreEvtText = True
                found = True
                choiceTemp.append(choice)
        #进行文本匹配后,如果存在的话,就将combobox的内容置为匹配到的列表,再弹出下拉菜单
        if found:
            print choiceTemp[0]
            self.SetItems(choiceTemp)
            self.Popup()
            self.SetValue(currentText)
            self.SetInsertionPoint(len(currentText))
            self.ignoreEvtText = False
        if not found:
            self.Dismiss()
            self.SetInsertionPoint(len(currentText))
            event.Skip()

class TrialPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, wx.ID_ANY)

        choices = [u'中国', u'中文', u'中美', 'aunt', 'uncle', 'grandson', 'granddaughter']

        cb = PromptingComboBox(self, "", choices, style=wx.CB_DROPDOWN) 

    def derivedRelatives(self, relative):
        return [relative, 'step' + relative, relative + '-in-law']


if __name__ == '__main__':
    app = wx.App()
    frame = wx.Frame (None, -1, 'Demo PromptingComboBox Control', size=(400, 200))
    TrialPanel(frame)
    frame.Show()
    app.MainLoop()

以上就是全部的代码了。但是每输入一次就会闪烁一下,这还是有点小问题。

这里写图片描述

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
你可以使用 wxPython 的 wx.AutoCompleteTextCtrl 类来实现自动提示文本框。它提供了一个自动完成功能,可以根据用户输入的内容来显示可能的匹配项。 下面是一个简单的示例代码,用于创建一个带有自动提示功能的文本框: ``` import wx class AutoCompleteTextCtrl(wx.TextCtrl): def __init__(self, parent, choices): wx.TextCtrl.__init__(self, parent, style=wx.TE_PROCESS_ENTER) self.choices = choices self.autoComplete() def autoComplete(self): completer = wx.AutoCompleteSimple(self.choices) completer.SetIgnoreCase(True) completer.AutoCompleter = wx.AutoCompletePopup() completer.AutoCompleter.SetMaxHeight(200) completer.AutoCompleter.SetHighlight(True) completer.AutoCompleter.SetIgnoreCase(True) completer.AutoCompleter.UseAutoComplete(False) completer.AutoCompleter.SetCompletionMode(wx.COMPLETE_ON_LAST) self.Bind(wx.EVT_TEXT_ENTER, self.onEnter) self.Bind(wx.EVT_TEXT, self.onText) def onEnter(self, event): self.SetValue(self.GetValue().capitalize()) self.SetInsertionPointEnd() def onText(self, event): self.autoComplete() if __name__ == '__main__': app = wx.App() frame = wx.Frame(None, title='AutoCompleteTextCtrl') panel = wx.Panel(frame) choices = ['apple', 'banana', 'cherry', 'grape'] textctrl = AutoCompleteTextCtrl(panel, choices) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(textctrl, 0, wx.EXPAND|wx.ALL, 10) panel.SetSizer(sizer) frame.Show() app.MainLoop() ``` 在这个示例中,我们创建了一个 AutoCompleteTextCtrl 类,它继承了 wx.TextCtrl 类,并使用了 wx.AutoCompleteSimple 和 wx.AutoCompletePopup 类来实现自动提示功能。在构造函数中,我们传入了一个选项列表,用于在用户输入时提供可能的匹配项。 然后,我们定义了两个事件处理函数:onEnter 和 onText。onEnter 函数在用户按下回车键时被调用,将文本框中的内容转换为大写形式,并将光标移动到文本框的末尾。onText 函数在用户每次输入时被调用,以更新可能的匹配项。 最后,在 if __name__ == '__main__' 代码块中,我们创建了一个 wxPython 应用程序,并在窗口中显示了一个 AutoCompleteTextCtrl 对象。你可以根据需要修改这个示例代码,以满足你的实际需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值