这是我的.py文件
import os
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty, ListProperty
import json
class LibraryMain(BoxLayout):
search_input = ObjectProperty()
search_results = ListProperty()
def searched_location(self):
print(self.search_results)
newis = ['1', '2', '3', '4']
self.search_results.extend(newis)
print(self.search_results)
print(f"""Searched for: {self.search_input} {self.search_results}""")
class LibraryApp(App):
pass
if __name__ == '__main__':
LibraryApp().run()
还有我的.kv文件
LibraryMain:
<LibraryMain>:
orientation: "vertical"
search_input: search_box.text
search_results: search_results_list.item_strings
BoxLayout:
height: "40dp"
size_hint_y: None
TextInput:
id: search_box
size_hint_x: 50
multiline: False
on_text_validate: root.searched_location()
Button:
text: "Search"
size_hint_x: 25
on_press: root.searched_location()
Button:
text: "Current Location"
size_hint_x: 25
on_press: root.searched_location()
ListView:
id: search_results_list
item_strings: ['a']
函数为ListProperty分配一个新值,这个新值应该传递到搜索结果中_list.item\u字符串在ListView中显示,但它没有,我也不知道为什么。你知道吗
有人能告诉我我缺少什么吗?你知道吗
在以下表达式中:
如果设置了绑定如果
item_strings
被更新,那么search_results
将用这些新值更新,但这并不意味着如果search_results
被修改item_strings
将被通知和更新。你知道吗你必须做的是相反的,我显示如下:
*.py
*.kv
总之,以下表达式:
指示修改
obj_b
的property_b
,然后obj_a
的property_a
将被更新,反之亦然。你知道吗解释
ListView的项字符串没有更新,因为下面的语句意味着搜索结果被分配了值来自搜索结果_list.item\u字符串。你知道吗
id()函数将显示两个变量(搜索结果和列表视图的项字符串)不指向同一列表对象。因此,更新搜索结果时,不会更新项字符串。你知道吗
解决方案
Python文件
kv文件
声明了两个根小部件。删除LibraryMain:
LibraryMain:#这是一个根小部件
<;LibraryMain>;:#这是一条规则
示例
你知道吗主.py你知道吗
kv文件
输出
相关问题 更多 >
编程相关推荐