簡體   English   中英

Kivy Recycleview 不更新添加的數據

[英]Kivy Recycleview doesn't update added data

我再次陷入了我的小 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'

您必須將新文件夾添加到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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM