我有一个使用kivy语言的python脚本,在这里我想不断更新屏幕的元素。
我写了一个代码,显示文件夹中的当前文件。我从github kivy页面上找到了一个示例,但是,该示例不是使用.kv文件开发的,主要原因是因为我必须将其添加到以.kv文件启动的另一个项目中。
以下是脚本:list_reset_data.py
如何翻译此代码以使其与.kv文件配合使用?。
这是我的代码,当前没有.kv文件即可工作:
# -*- coding: utf-8 -*-
from kivy.uix.listview import ListView
from kivy.uix.floatlayout import FloatLayout
from kivy.clock import Clock
from kivy.adapters.listadapter import ListAdapter
from kivy.adapters.models import SelectableDataItem
import subprocess
from kivy.uix.listview import ListItemButton
import random
class DataItem(SelectableDataItem):
def __init__(self, name, **kwargs):
self.name = name
super(DataItem, self).__init__(**kwargs)
class MainView(FloatLayout):
"""
Implementation of a ListView using the kv language.
"""
list_2 = []
def __init__(self, **kwargs):
super(MainView, self).__init__(**kwargs)
data_items = []
list_item_args_converter = lambda row_index, obj: {'text': obj.name,
'size_hint_y': None,
'height': 25}
self.list_adapter = \
ListAdapter(data=data_items,
args_converter=list_item_args_converter,
selection_mode='single',
propagate_selection_to_data=False,
allow_empty_selection=False,
cls=ListItemButton)
self.list_view = ListView(adapter=self.list_adapter)
self.add_widget(self.list_view)
self.toggle = 'adding'
Clock.schedule_interval(self.update_list_data, 1)
def update_list_data(self, dt):
list_1 = []
index = -1
out = subprocess.check_output("ls /home/pi/Documents/myRoutes", shell = True)
sp = out.split(".xlsx")
for i in range(len(sp)):
if(sp[i] != '\n'):
list_1.append(sp[i].strip())
if len(list_1) > len(MainView.list_2):
for i in list_1:
for x in MainView.list_2:
if i == x: #in x:
#print("item from list 1 {} is in list 2 {}".format(i, x))
break
else:
#print("item from list 1 {} is NOT in list 2".format(i))
items = self.list_adapter.data
item = DataItem(i)
items.append(item)
MainView.list_2.append(i)
if len(list_1) < len(MainView.list_2):
print("current is smaller ")
for i in MainView.list_2: # list 1
for x in list_1: # list 2
if i == x: #in x:
index += 1
#print("item from list 1 {} is in list 2 {} and the index is {}".format(i, x, index))
break
else:
index += 1
print("item from list 1 {} is NOT in list 2 and I have to delete it and the index is {}".format(i,index))
items = self.list_adapter.data
item = DataItem(i)
del items[index]
del MainView.list_2[index]
print("new {}".format(list_1))
print("")
print("old {}".format(MainView.list_2))
print("")
if __name__ == '__main__':
from kivy.base import runTouchApp
runTouchApp(MainView(width=800))
如果发现有帮助,此代码可以在列表中为您提供文件夹中具有特定扩展名的元素。致以诚挚的问候,我希望能获得任何线索以了解如何做到这一点。
答案 0 :(得分:0)
在youtube上尝试使用本教程中的示例后:Kivy Tutorial 4: Kivy ListView & ListAdapter
这是我要完成的工作代码。 studentdb.py:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.uix.listview import ListItemButton
from kivy.clock import Clock
import subprocess
class StudentListButton(ListItemButton):
pass
class StudentDB(BoxLayout):
student_list = ObjectProperty()
cnt = 0
list_2 = []
def __init__(self, **kwargs):
super(StudentDB, self).__init__(**kwargs)
Clock.schedule_interval(self.update_list_data, 1)
def update_list_data(self, dt):
list_1 = []
index = -1
#out = subprocess.check_output("ls /home/username/Documents/myRoutes", shell=True) # test for Lubuntu
out = subprocess.check_output("ls /home/pi/Documents/myRoutes", shell=True) # test for RPi
sp = out.split(".xlsx")
for i in range(len(sp)):
if(sp[i] != '\n'):
list_1.append(sp[i].strip())
if len(list_1) > len(StudentDB.list_2):
for i in list_1:
for x in StudentDB.list_2:
if i == x: # in x:
#print("item from list 1 {} is in list 2 {}".format(i, x))
break
else:
#print("item from list 1 {} is NOT in list 2".format(i))
self.student_list.adapter.data.extend([i])
StudentDB.list_2.append(i)
if len(list_1) < len(StudentDB.list_2):
print("current is smaller ")
for i in StudentDB.list_2: # list 1
for x in list_1: # list 2
if i == x: #in x:
index += 1
#print("item from list 1 {} is in list 2 {} and the index is {}".format(i, x, index))
break
else:
index += 1
print("item from list 1 {} is NOT in list 2 and I have to delete it and the index is {}".format(i,index))
selection = i
self.student_list.adapter.data.remove(selection)
del StudentDB.list_2[index]
studentdb.kv:
# Reference studentdb.py
#: import main studentdb
#: import ListAdapter kivy.adapters.listadapter.ListAdapter
#: import ListItemButton kivy.uix.listview.ListItemButton
StudentDB:
<StudentDB>:
orientation: "vertical"
student_list: students_list_view
padding: 10
spacing: 10
BoxLayout:
size_hint_y: None
height: "40dp"
BoxLayout:
size_hint_y: None
height: "40dp"
# Define starting data and point to the ListItemButton
# in the Python code
ListView:
id: students_list_view
adapter:
ListAdapter(data = [], cls=ListItemButton)