gpt4 book ai didi

python - 在 kivy 中有建议的搜索框?

转载 作者:行者123 更新时间:2023-12-03 15:00:28 27 4
gpt4 key购买 nike

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

最佳答案

这是一个简单的示例,它将使用使用文本输入在 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()

关于python - 在 kivy 中有建议的搜索框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33073426/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com