如何用Kivy更新ListView?蟒蛇

时间:2018-02-07 20:00:48

标签: python kivy kivy-language

我已经在其他帖子中看到了这个问题,但我无法在我发现和尝试过的事情上取得任何进展。

我有一个Kivy应用程序,可以将数据保存在一个特定文件夹中,还具有从此文件夹加载以前保存的数据的功能。我可以看到这些数据的列表,但我的问题是我无法更新当前列表以保存新信息。

我需要重建脚本以查看文件夹中的新文件。

我如何使用我开发的当前代码执行此操作?我需要添加什么?我将分享一些代码来展示我的内容。

蟒:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty, StringProperty, NumericProperty, ListProperty
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
import os, sys, os.path


class Screen_Management(ScreenManager):
    pass


class Routes_List_Screen(Screen):
    #path = "/home/pi/Documents/myRoutes"
    path = "C:\Users\my_user\Documents\myRoutes"
    dirs = os.listdir(path)
    my_data = ListProperty(dirs)
    list_item = ObjectProperty()


class Race_Setup_Screen(Screen):
    pass

presentation = Builder.load_file("gui.kv")

class guiApp(App):

    def build(self):
        return presentation


guiApp().run()

kv档案:

#: import ListAdapter kivy.adapters.listadapter.ListAdapter
#: import ListItemButton kivy.uix.listview.ListItemButton

Screen_Management:
    id: screen_management
    Race_Setup_Screen:
        name: "race_setup_screen_name"
    Routes_List_Screen:
        name: "route_list_name"
        manager: screen_management


<Race_Setup_Screen>:

    Button:
        text: "ROUTE LIST"
        on_release: app.root.current = "route_list_name"
        # I think I can put a call to a function here that 
        # will update the current file list.

# Screen 6: Route List Screen
<Routes_List_Screen>:
    list_item: data_list_view

    BoxLayout:
        orientation: 'vertical'
        padding: 1
        Button:
            size_hint: [1, .1]
            font_size: 20
            text: 'DATALOGGER'
        ListView:
            id: data_list_view
            size_hint_y: .8
            adapter:
                # Here is the Adapter
                ListAdapter(data=root.my_data,
                selection_mode='single',
                allow_empty_selection=False,
                cls=ListItemButton)
        BoxLayout:
            orientation: 'horizontal'
            size_hint: [1, .2]
            Button:
                text: 'BACK'
                on_release: app.root.current = "race_setup_screen_name"
            Button:
                text: 'LOAD'
                on_release: app.root.current = "race_setup_screen_name"

我希望得到你们的帮助,谢谢你的时间!

1 个答案:

答案 0 :(得分:1)

一种可能的解决方案是使用在显示新屏幕之前执行的on_pre_enter事件来加载新目录

=2