如何在 Python Kivy 的 JsonStore() 中的第一项中.put() 数据
How to .put() data in the first item in a JsonStore() in Python Kivy
我正在创建一个应用程序,其中涉及 table 使用 Kivy 和模块 storage。每次我在 Json 文件中 .put()
某些内容时,我希望某些数据出现在 table 的标题下方。意思是我希望 table 首先显示最新数据。我显示 table 的代码是这样的:
table.add_widget(Label(text="Heading 1"))
table.add_widget(Label(text="Heading 2"))
table.add_widget(Label(text="Heading 3"))
for item in database:
data1= database.get(item)['data1']
data2 = database.get(item)['data2']
data3 = database.get(item)['data3']
table.add_widget(Label(text=data1))
table.add_widget(Label(text=data2))
table.add_widget(Label(text=data3))
备注:
table
是网格布局
database
是 Json 文件
我的第一个解决方案是 reversed()
for-loop 但它不起作用,因为 database
不是字典也不是列表,因为它是 JsonStore()
。
我的第二个解决方案是.put()
像这样Json文件前面的数据:
{"added_data": {"data1": "info1", "data2": "info2", "data3": "info3"}, "old_data": {"data1": "info1", "data2": "info2", "data3": "info3"}}
但我不知道该怎么做。
所以...你.put()
前面的数据怎么弄的?或者还有其他方法吗?
add_widget
方法接受一个 index
参数,它允许您决定在哪里放置新的 Widget,因此您可以在选定的位置插入新条目而不是尝试更改数据的顺序。示例代码:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
kv = '''
#:import Label kivy.uix.label.Label
<TestWidget>
orientation: 'vertical'
BoxLayout:
orientation: 'horizontal'
Button:
text: 'add before'
on_press: root.add_widget(Label(text="before"), index=len(root.children))
Button:
text: 'add after'
on_press: root.add_widget(Label(text="after"))
'''
Builder.load_string(kv)
class TestWidget(BoxLayout):
pass
class TestApp(App):
def build(self):
return TestWidget()
if __name__ == '__main__':
TestApp().run()
带有 GridLayout
table 的示例:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.gridlayout import GridLayout
kv = '''
#:import Label kivy.uix.label.Label
#:import randint random.randint
<TestWidget>
cols: 3
Button:
text: 'add first'
on_press:
root.add_widget(Label(text=str(randint(0, 10))), index=len(root.children)-3)
root.add_widget(Label(text=str(randint(0, 10))), index=len(root.children)-3)
root.add_widget(Label(text=str(randint(0, 10))), index=len(root.children)-3)
Widget
Button:
text: 'add last'
on_press:
root.add_widget(Label(text=str(randint(0, 10))))
root.add_widget(Label(text=str(randint(0, 10))))
root.add_widget(Label(text=str(randint(0, 10))))
'''
Builder.load_string(kv)
class TestWidget(GridLayout):
pass
class TestApp(App):
def build(self):
return TestWidget()
if __name__ == '__main__':
TestApp().run()
我正在创建一个应用程序,其中涉及 table 使用 Kivy 和模块 storage。每次我在 Json 文件中 .put()
某些内容时,我希望某些数据出现在 table 的标题下方。意思是我希望 table 首先显示最新数据。我显示 table 的代码是这样的:
table.add_widget(Label(text="Heading 1"))
table.add_widget(Label(text="Heading 2"))
table.add_widget(Label(text="Heading 3"))
for item in database:
data1= database.get(item)['data1']
data2 = database.get(item)['data2']
data3 = database.get(item)['data3']
table.add_widget(Label(text=data1))
table.add_widget(Label(text=data2))
table.add_widget(Label(text=data3))
备注:
table
是网格布局database
是 Json 文件
我的第一个解决方案是 reversed()
for-loop 但它不起作用,因为 database
不是字典也不是列表,因为它是 JsonStore()
。
我的第二个解决方案是.put()
像这样Json文件前面的数据:
{"added_data": {"data1": "info1", "data2": "info2", "data3": "info3"}, "old_data": {"data1": "info1", "data2": "info2", "data3": "info3"}}
但我不知道该怎么做。
所以...你.put()
前面的数据怎么弄的?或者还有其他方法吗?
add_widget
方法接受一个 index
参数,它允许您决定在哪里放置新的 Widget,因此您可以在选定的位置插入新条目而不是尝试更改数据的顺序。示例代码:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
kv = '''
#:import Label kivy.uix.label.Label
<TestWidget>
orientation: 'vertical'
BoxLayout:
orientation: 'horizontal'
Button:
text: 'add before'
on_press: root.add_widget(Label(text="before"), index=len(root.children))
Button:
text: 'add after'
on_press: root.add_widget(Label(text="after"))
'''
Builder.load_string(kv)
class TestWidget(BoxLayout):
pass
class TestApp(App):
def build(self):
return TestWidget()
if __name__ == '__main__':
TestApp().run()
带有 GridLayout
table 的示例:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.gridlayout import GridLayout
kv = '''
#:import Label kivy.uix.label.Label
#:import randint random.randint
<TestWidget>
cols: 3
Button:
text: 'add first'
on_press:
root.add_widget(Label(text=str(randint(0, 10))), index=len(root.children)-3)
root.add_widget(Label(text=str(randint(0, 10))), index=len(root.children)-3)
root.add_widget(Label(text=str(randint(0, 10))), index=len(root.children)-3)
Widget
Button:
text: 'add last'
on_press:
root.add_widget(Label(text=str(randint(0, 10))))
root.add_widget(Label(text=str(randint(0, 10))))
root.add_widget(Label(text=str(randint(0, 10))))
'''
Builder.load_string(kv)
class TestWidget(GridLayout):
pass
class TestApp(App):
def build(self):
return TestWidget()
if __name__ == '__main__':
TestApp().run()