I can't figure out how to print the whole output out of a loop from tested Kivy Minimal Reproducible Example below:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class DataTable(BoxLayout):
def __init__(self,table='', **kwargs):
super().__init__(**kwargs)
def build(self):
datas = {}
for i in range(3):
data = {
'1':{i:'TESTa'},
'2':{i:'TESTb'},
'3':{i:'TESTc'},
} #data store
datas.update(data)
print(datas)
test = build(self)
class DataTableApp(App):
def build(self):
return DataTable()
if __name__=='__main__':
DataTableApp().run()
It outputs the last dictionary item only as:
{'1': {2: 'TESTa'}, '2': {2: 'TESTb'}, '3': {2: 'TESTc'}}
While with the
print(datas)
as this
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class DataTable(BoxLayout):
def __init__(self,table='', **kwargs):
super().__init__(**kwargs)
def build(self):
datas = {}
for i in range(3):
data = {
'1':{i:'TESTa'},
'2':{i:'TESTb'},
'3':{i:'TESTc'},
} #data store
datas.update(data)
print(datas)
test = build(self)
class DataTableApp(App):
def build(self):
return DataTable()
if __name__=='__main__':
DataTableApp().run()
inside the for loop scope it does print the whole dictionary as:
{'1': {0: 'TESTa'}, '2': {0: 'TESTb'}, '3': {0: 'TESTc'}}
{'1': {1: 'TESTa'}, '2': {1: 'TESTb'}, '3': {1: 'TESTc'}}
{'1': {2: 'TESTa'}, '2': {2: 'TESTb'}, '3': {2: 'TESTc'}}
I tried as per those answers (initializing the empty dictionary variable datas = {} outside and above the for loop scope) :
https://stackoverflow.com/a/65779535/10789707
https://stackoverflow.com/a/66901779/10789707
https://stackoverflow.com/a/61539025/10789707
https://stackoverflow.com/a/56638281/10789707
I was Expecting the 2nd output above when the print statement print(datas) is outside the loop scope:
{'1': {0: 'TESTa'}, '2': {0: 'TESTb'}, '3': {0: 'TESTc'}}
{'1': {1: 'TESTa'}, '2': {1: 'TESTb'}, '3': {1: 'TESTc'}}
{'1': {2: 'TESTa'}, '2': {2: 'TESTb'}, '3': {2: 'TESTc'}}
The end use would be to then use the whole dictionary (not just the last key value pair pair) to display in the Kivy window app as per the source example from github script:
https://github.com/mfazrinizar/Datatable-Kivy/blob/master/datatable.py
The data dictionary keys values pairs must all be accessible outside of the loop in order to then display them in the Kivy window.
Here's other documentation and resources I've consulted:
https://realpython.com/iterate-through-dictionary-python/
for loop printing out last item in my array only
Why python only printing the last key from dictionary?
Append a dictionary to a dictionary
https://www.youtube.com/watch?v=nCdcFOLww_c
https://www.geeksforgeeks.org/iterate-over-a-dictionary-in-python/
Python dictionary only keeps information from last iteration of loop