在 kivy 中有建议的搜索框?

Posted

技术标签:

【中文标题】在 kivy 中有建议的搜索框?【英文标题】:Search Box with suggestions in kivy? 【发布时间】:2016-01-09 11:28:00 【问题描述】:

我是 kivy 的新手,想知道我们如何将文本输入框绑定到建议,以便用户触摸并选择建议。 我有一长串按钮,我想根据名称从中选择。

【问题讨论】:

【参考方案1】:

这是一个简单的示例,它将使用使用文本输入在option_list 中进行搜索,并将在文本输入小部件下显示建议。 我使用kivymd 小部件来获得漂亮的外观设计,如果你愿意,你可以用普通的 kivy 小部件替换

from kivy.properties import ListProperty
from kivymd.app import MDApp
from kivymd.uix.list import OneLineAvatarIconListItem
from kivymd.uix.textfield import MDTextField

kv = """

Screen:
    BoxLayout:
        orientation: 'vertical'
        spacing: 1
        BoxLayout:
            size_hint_y: 1/5
            canvas.before:
                Color:
                    rgba:  0, 0, 0, 1
                Rectangle:
                    pos: self.pos
                    size: self.size[0], 2
            MDIconButton:
                icon: 'magnify'
                size_hint_y: 1
            SearchTextInput:
                id: Search_TextInput_id
                size_hint_y: .97
                pos_hint: 'left':0 , 'top': 1
                hint_text: 'search'
                hint_text_color: 1,1,1,1
                icon_left: 'magnify'
                mode: "fill"
                helper_text_mode: "persistent"
                helper_text: "Search"
                line_color: [1,1,1,1]
                color_normal: [1,1,1,1]

                font_size: .35 * self.height
                active_line: False
                multiline: False
 
            MDIconButton:
                icon: 'close'
                size_hint_y:1
                text_color: 0,0,0,1

        BoxLayout:
            orientation: 'vertical'
            padding: 4
            RecycleView:
                viewclass: 'Search_Select_Option'
                data:app.rv_data
                RecycleBoxLayout:
                    spacing: 15
                    padding : 10
                    default_size: None, None
                    default_size_hint: 1, None
                    size_hint_y: None
                    height: self.minimum_height
                    orientation: 'vertical'
<Search_Select_Option>:
    on_release: print(self.text)
    IconRightWidget:
        icon: "arrow-top-left"
"""


class Search_Select_Option(OneLineAvatarIconListItem):
    pass


class SearchTextInput(MDTextField):
    option_list = 'one1,two1,two2,three1,three2,three3,four1,four2,four3,four4,five1,five2,five3,five4,five5'.split(',')

    def on_text(self, instance, value):
        app = MDApp.get_running_app()
        option_list = list(set(self.option_list + value[:value.rfind(' ')].split(' ')))
        val = value[value.rfind(' ') + 1:]
        if not val:
            return
        try:
            app.option_data = []
            for i in range(len(option_list)):
                word = [word for word in option_list if word.startswith(val)][0][len(val):]
                if not word:
                    return
                if self.text + word in option_list:
                    if self.text + word not in app.option_data:
                        popped_suggest = option_list.pop(option_list.index(str(self.text + word)))
                        app.option_data.append(popped_suggest)
                app.update_data(app.option_data)

        except IndexError:

            pass


class RVTestApp(MDApp):
    rv_data = ListProperty()

    def update_data(self, rv_data_list):
        self.rv_data = ['text': item for item in rv_data_list]
        print(self.rv_data, 'update')

    def build(self):
        return Builder.load_string(kv)


RVTestApp().run() 

【讨论】:

【参考方案2】:

我正在使用 kivymd,我真的不知道如何在 kivy 中使用,但这里是 kivymd 的代码

from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.uix.screenmanager import Screen

from kivymd.icon_definitions import md_icons
from kivymd.app import MDApp
from kivymd.uix.list import OneLineIconListItem


Builder.load_string(
'''
#:import images_path kivymd.images_path


<CustomOneLineIconListItem>:

  IconLeftWidget:
     icon: root.icon


<PreviousMDIcons>:

  BoxLayout:
    orientation: 'vertical'
    spacing: dp(10)
    padding: dp(20)

    BoxLayout:
        size_hint_y: None
        height: self.minimum_height

        MDIconButton:
            icon: 'magnify'

        MDTextField:
            id: search_field
            hint_text: 'Search icon'
            on_text: root.set_list_md_icons(self.text, True)

    RecycleView:
        id: rv
        key_viewclass: 'viewclass'
        key_size: 'height'

        RecycleBoxLayout:
            padding: dp(10)
            default_size: None, dp(48)
            default_size_hint: 1, None
            size_hint_y: None
            height: self.minimum_height
            orientation: 'vertical'
 '''
 )


class CustomOneLineIconListItem(OneLineIconListItem):
     icon = StringProperty()


class PreviousMDIcons(Screen):

    def set_list_md_icons(self, text="", search=False):
      '''Builds a list of icons for the screen MDIcons.'''

     def add_icon_item(name_icon):
        self.ids.rv.data.append(
            
                "viewclass": "CustomOneLineIconListItem",
                "icon": name_icon,
                "text": name_icon,
                "callback": lambda x: x,
            
        )

    self.ids.rv.data = []
    for name_icon in md_icons.keys():
        if search:
            if text in name_icon:
                add_icon_item(name_icon)
        else:
            add_icon_item(name_icon)


class MainApp(MDApp):
  def __init__(self, **kwargs):
     super().__init__(**kwargs)
     self.screen = PreviousMDIcons()

  def build(self):
      return self.screen

  def on_start(self):
      self.screen.set_list_md_icons()


MainApp().run()

一些结果 here is the screen shot result when your not searching for anything

here is a screen shot when i am trying to search

second

here is a result when the word am searching does not exist

【讨论】:

我们如何从列表中捕获选择?例如,从您关于 microsoft 的示例中,我们如何捕获“microsoft-dynamics”(文本)以便我们可以引用它?

以上是关于在 kivy 中有建议的搜索框?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Angular 2/4 开发搜索建议文本框

一个简单的ExtJS搜索建议框

Angular 2/4 自动完成搜索框

通过在下拉列表中选择数据库列调用 jsp 进行自动建议搜索框

带有用户表单上的建议列表的可搜索组合框

Angular 高级搜索框不显示建议值