Reputation: 119
I'm creating an app which involves a table using Kivy with the module storage. Every time I .put()
something in the Json file, I want that certain data to appear just below the headings of the table. Meaning I want the table to show first the most recent data. My code that displays the table is like this:
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))
Notes:
table
is a Gridlayoutdatabase
is the Json fileMy first solution is to reversed()
the for-loop but it will not work because database
is not a dictionary nor a list because it is a JsonStore()
.
My second solution is to .put()
the data in front of the Json file like this:
{"added_data": {"data1": "info1", "data2": "info2", "data3": "info3"}, "old_data": {"data1": "info1", "data2": "info2", "data3": "info3"}}
but I don't know how to do this.
So... how do you .put()
the data in front? or is there any other way to do this?
Upvotes: 0
Views: 919
Reputation: 8747
add_widget
method accepts an index
argument which allows you to decide where to put new Widget, so instead to try changing order of data you can just insert new entries in a selected place. An example code:
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()
An example with a 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()
Upvotes: 1