Kivy ListView不会更新项目字符串

2024-10-01 07:35:03 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的.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中显示,但它没有,我也不知道为什么。你知道吗

List item 'a' shows, but '1', '2', '3', '4' are missing

有人能告诉我我缺少什么吗?你知道吗


Tags: textfromimportselfinputsearchsizelocation
2条回答

在以下表达式中:

search_results: search_results_list.item_strings

如果设置了绑定如果item_strings被更新,那么search_results将用这些新值更新,但这并不意味着如果search_results被修改item_strings将被通知和更新。你知道吗

你必须做的是相反的,我显示如下:

*.py

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty, ListProperty


class LibraryMain(BoxLayout):
    search_input = ObjectProperty()
    search_results = ListProperty(['a'])

    def __init__(self, **kwargs):
        BoxLayout.__init__(self, **kwargs)

    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
    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: root.search_results  # < 

总之,以下表达式:

obj_a:
    property_a : obj_b.property_b

指示修改obj_bproperty_b,然后obj_aproperty_a将被更新,反之亦然。你知道吗

obj_a.property_a < - obj_b.property_b

解释

ListView的项字符串没有更新,因为下面的语句意味着搜索结果被分配了来自搜索结果_list.item\u字符串。你知道吗

search_results: search_results_list.item_strings  

id()函数将显示两个变量(搜索结果列表视图的项字符串)不指向同一列表对象。因此,更新搜索结果时,不会更新项字符串。你知道吗

解决方案

Python文件

  1. 搜索结果列表属性更改为对象属性
  2. 更换self.search\u结果self.search\u结果.item\u字符串
  3. def build(self)替换pass:return LibraryMain()

kv文件

  1. 替换搜索结果:搜索结果_list.item\u字符串搜索结果:搜索结果列表
  2. 声明了两个根小部件。删除LibraryMain:

    LibraryMain:#这是一个根小部件

    <;LibraryMain>;:#这是一条规则

示例

你知道吗主.py你知道吗

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty


class LibraryMain(BoxLayout):

    search_input = ObjectProperty(None)
    search_results = ObjectProperty(None)

    def searched_location(self):
        print(self.search_results.item_strings)
        newis = ['1', '2', '3', '4']
        self.search_results.item_strings.extend(newis)
        print(self.search_results.item_strings)
        print(f"""Searched for: {self.search_input} {self.search_results.item_strings}""")


class LibraryApp(App):

    def build(self):
        return LibraryMain()


if __name__ == '__main__':
    LibraryApp().run()

kv文件

#:kivy 1.10.0

<LibraryMain>:
    orientation: "vertical"
    search_input: search_box.text
    search_results: search_results_list
    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']

输出

Img01 - ListView's item_strings updated

相关问题 更多 >