【问题标题】:Kivy Recycleview doesn't update added dataKivy Recycleview 不更新添加的数据
【发布时间】:2021-01-14 06:59:28
【问题描述】:

我又一次被我的小 Kivy 项目困住了。 想法是该应用程序从 Recycleview 列表中的目录中列出文件夹名称。我希望能够在该目录中添加一个包含应用程序的文件夹,并在创建文件夹后更新列表。

显示列表和创建文件夹工作。但我无法让 Recycleview 更新。我尝试了我在网上找到的所有方法,但我认为主要问题是我可能不完全了解不同 id 之间的关系以及如何将更新的列表转发到 RV。我知道这里已经介绍过很多次了,但我就是想不通。

非常感谢所有花时间查看我的问题的人。

我总结的 Python 文件:

import os
from pathlib import Path
from kivy.app import App
import pathlib
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.properties import ListProperty, StringProperty, ObjectProperty, BooleanProperty
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.uix.popup import Popup
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.boxlayout import BoxLayout

prjct_list = []
prjct_list_clean = []

entries = pathlib.Path.home() / 'Desktop' / 'Troubleshootdir'

def get_all_files(entries):
    prjct_list = []
    global prjct_list_clean
    file_iterator = entries.iterdir()
    for entry in sorted(file_iterator):
        if entry.is_dir():
            prjct_list.append(entry.name)
            prjct_list_clean = list(filter(lambda x: x.startswith(''), prjct_list))
    return prjct_list_clean

class MessageBox(Popup):

    def popup_dismiss(self):
        self.dismiss()

class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior, RecycleBoxLayout):
    """ Adds selection and focus behaviour to the view. """
    selected_value = StringProperty('')
    btn_info = ListProperty(prjct_list_clean)

class SelectableButton(RecycleDataViewBehavior, Button, Widget):
    """ Add selection support to the Label """
    index = None
    selected = BooleanProperty(False)
    selectable = BooleanProperty(True)

    def refresh_view_attrs(self, rv, index, data):
        """ Catch and handle the view changes """
        self.index = index
        return super(SelectableButton, self).refresh_view_attrs(rv, index, data)


#############################################   RV   #############################################
class RV(RecycleView):
    rv_layout = ObjectProperty(None)

    def __init__(self, **kwargs):
        global prjct_list_clean
        get_all_files(entries)
        super(RV, self).__init__(**kwargs)
        self.data = []
        for subject in prjct_list_clean:
            self.data.append({'text':subject})
        rv = self.ids['rv_layout']
        rv.data = self.data

    def callback(self, text):
        self.ids.con_wksp.text += text

class Manager(Widget):
    new_wksp = ObjectProperty(None)

    def cre_wksp(self):
        path = Path(entries, 'Prfx_' + self.new_wksp.text, "Subfolder01/Subfolder02")
        os.makedirs(path, exist_ok=True)
        path = Path(entries, 'Prfx_' + self.new_wksp.text, "01_" + self.new_wksp.text + ".APN", 'Subfolder02')
        os.makedirs(path, exist_ok=True)

class TroubleshootApp(BoxLayout, App,):
    def build(self):
        self.recycl = RV()
        self.add_widget(self.recycl)
        self.mnager = Manager()
        self.add_widget(self.mnager)
        return self

    def update_dir(self):
        get_all_files(entries)
        self.recycl.ids.rv_layout.data = prjct_list_clean

if __name__ == "__main__":
    TroubleshootApp().run()

我的 .kv 文件:

#:kivy 2.0.0

<SelectableButton>:
    # Draw a background to indicate selection
    background_color: [153 / 255.0, 153 / 255.0, 153 / 255.0, 255 / 255.0]
    canvas.before:
        Color:
            rgba: [230 / 255.0, 115 / 255.0, 0 / 255.0, 255 / 255.0]
        Rectangle:
            pos: self.pos
            size: self.size

<RV>:
    #rv_layout: rv_layout
    bar_width: 0
    viewclass: 'SelectableButton'
    SelectableRecycleBoxLayout:
        id: rv_layout
        default_size: None, dp(56)
        default_size_hint: 0.9, None
        size_hint_y: None
        height: self.minimum_height
        orientation: "vertical"

<Manager>:

    new_wksp: new_wksp

    FloatLayout:
        pos: 0, 0
        size: root.width, root.height

        Button:
            text: 'Create Folder'
            x: root.x * 1.1
            top: self.height and root.y + root.height * 0.5
            size_hint: 0.8, 0.15
            on_press:
                root.cre_wksp()

        TextInput:
            id: new_wksp
            x: root.x * 1
            top: self.height and root.y + root.height * 0.9
            size_hint: 1, 0.15
            padding: 0, (self.height-self.line_height)/2
            halign: 'center'

【问题讨论】:

    标签: python python-3.x kivy


    【解决方案1】:

    您必须将新文件夹添加到您的 RV 数据中。只需在 cre_wksp() 方法的末尾添加对 update_dir() 的调用即可:

    def cre_wksp(self):
        path = Path(entries, 'Prfx_' + self.new_wksp.text, "Subfolder01/Subfolder02")
        os.makedirs(path, exist_ok=True)
        path = Path(entries, 'Prfx_' + self.new_wksp.text, "01_" + self.new_wksp.text + ".APN", 'Subfolder02')
        os.makedirs(path, exist_ok=True)
        App.get_running_app().update_dir()
    

    update_dir() 方法进行了一些更改:

    def update_dir(self):
        get_all_files(entries)
        data = []
        for subject in prjct_list_clean:
            data.append({'text':subject})
        self.recycl.data = data
    

    【讨论】:

    • 约翰,再次感谢您的帮助。这样就成功了,刚才我让很多事情变得更清楚了。非常感谢您的帮助!
    猜你喜欢
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-01
    相关资源
    最近更新 更多