Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search Box with suggestions in kivy?

Tags:

python

kivy

I am new to kivy wanted to know how we can bind textinput box to suggestion so that user and touch and select the suggestions. I have long list of buttons out of which i want to select on the basis of name.

like image 221
pkm Avatar asked Oct 12 '15 04:10

pkm


People also ask

Can Kivy be used on a desktop application?

As it can be run on Android, IOS, linux and Windows etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications. In this article we will learn how we can add a button with the Text input in kivy, just like the same we have in the input and submit button.

How do I get help with Kivy?

Sign in to your account We use the issue tracker exclusively for bug reports and feature requests. However, this issue appears to be a support request. Please use our support channels to get help with the project. If you're having trouble installing Kivy, make sure to check out the installation docs for Windows, Linux and macOS.

What is Kivy in Python?

Kivy is a platform-independent GUI tool in Python. As it can be run on Android, IOS, linux and Windows etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications.

What are the parameters of Kivy window on_key_up ()?

The parameters are the same as kivy.core.window.WindowBase.on_key_up (). When overwriting the method in the derived widget, super should be called to enable de-focusing on escape. If the derived widget wishes to use escape for its own purposes, it can call super after it has processed the character (if it does not wish to consume the escape).


2 Answers

Here is a simple example that will use the use text input to search in the option_list and will display the suggestion under the text input widget. I used the kivymd widget to get a nice look design you replaced with the normal kivy widgets if you want

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() 
like image 83
Hussam F. Alkdary Avatar answered Oct 02 '22 09:10

Hussam F. Alkdary


i am using kivymd i dont real know how to do it in kivy but here are the codes for 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()

some results 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

like image 25
Aqeglipa beast Avatar answered Oct 02 '22 10:10

Aqeglipa beast