gpt4 book ai didi

python - 如何在 KIVY、python 中使用 RecycleView 动态加载数据

转载 作者:太空宇宙 更新时间:2023-11-03 21:06:32 25 4
gpt4 key购买 nike

我正在尝试将数据添加到 sqlite3 数据库,以便动态地从 RecycleView 中读取、添加和删除它们,但我无法在将数据上传到数据库后自动加载它们。

这是我的代码的一部分,我需要修改它才能工作。非常感谢。

producto=[]

def cargarproductosbase():
global producto
con = sqlite3.connect("base.db")
cur = con.cursor()
cur.execute("select nombre from producto")
print("se cargaron los productos")
rows = cur.fetchall()
for row in rows:
for i in row:
producto.append(i)
print(producto)
con.commit()
con.close()
print ("cargue estos datos")
print (producto)

class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior,
RecycleBoxLayout):
''' Adds selection and focus behaviour to the view. '''


class SelectableLabel(RecycleDataViewBehavior, Label):
''' Add selection support to the Label '''
index = None
selected = BooleanProperty(False)
selectable = BooleanProperty(True)
cols=2

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

def on_touch_down(self, touch):
''' Add selection on touch down '''
if super(SelectableLabel, self).on_touch_down(touch):
return True
if self.collide_point(*touch.pos) and self.selectable:
return self.parent.select_with_touch(self.index, touch)

def apply_selection(self, rv, index, is_selected):
''' Respond to the selection of items in the view. '''
self.selected = is_selected
if is_selected:
aceptar=AceptarDonacion()
aceptar.open()

print("selection changed to {0}".format(rv.data[index]))
else:
print("selection removed for {0}".format(rv.data[index]))


class ExampleRV(RecycleView):
global producto
cargarproductosbase()
def __init__(self, **kwargs):
global producto

super(ExampleRV, self).__init__(**kwargs)

Clock.schedule_interval(self.reloading, 1)

def reloading(self, nothing):
global producto
self.data = [{'text': str(x)} for x in producto]
print (producto)

最佳答案

在类 ExampleRV 的构造函数中实例化类 cargarproductosbase()

片段

class ExampleRV(RecycleView):

def __init__(self, **kwargs):
global producto
super(ExampleRV, self).__init__(**kwargs)
cargarproductosbase()
self.data = [{'text': str(x)} for x in producto]
print(producto)

输出

RV - data loaded

关于python - 如何在 KIVY、python 中使用 RecycleView 动态加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55357576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com