I am using a for loop to cycle the months of the year and then append them to a list rather than manually type out each month.
The variable self.mylist
updates mylist
perfectly fine.
When for i in range(1,13):
is run it updates self.mylist
perfectly fine.
But because self.mylist
isn't called again it doesn't update mylist
after the for loop. Or so i believe is my issue.
I think this method is necessary because ListProperty cannot be appended to but can be assigned to?
So my question is after the for loop is run how can i update mylist
again with self.mylist
The kv file includes only the relevant part of the problem. Which functions as intended, to grab a value from the list and display it with text:
.py file
class Telldate(AnchorLayout):
todayday= ObjectProperty('')
mylist=ListProperty(['','','','','','',''])
print(mylist)
def __init__(self,*args, **kwargs):
super().__init__(*args, **kwargs)
self.todayday=strftime('%A')
self.mylist= ['this', 'does','work', 'but']
print(self.mylist)
for i in range(1,13):
self.mylist.append(calendar.month_name[i])
print(self.mylist)
class PlannerApp(App):
# def updater(self):
# Clock.schedule_interval(self.monthcyclewithglobal, 0.5)
def build(self):
return Telldate()
if __name__ == '__main__':
PlannerApp().run()
.kv file
<Telldate>:
---------
-----
--
text:root.mylist[3]
Things I've tried but don't seem to work.
so I could define another function and use a return statement.
def monthcycle(self):
self.mylist= ['this', 'does','work', 'but']
print(self.mylist)
for i in range(1,13):
self.mylist.append(calendar.month_name[i])
print(self.mylist)
return self.mylist
Or I could use global variables which doesn't seem to encouraged
def monthcyclewithglobal():
global mylist
mylist= ['this', 'does','work', 'but']
print(mylist)
for i in range(1,13):
mylist.append(calendar.month_name[i])
print(mylist)
monthcyclewithglobal() #I am aware this bit is probably terrible code
Hard coding the months in works fine. But I would like automation.
Like so
self.mylist= [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', \
'August', 'September', 'October', 'November', 'December']
kivy V1.10.0 python V3.6.2 using IDLE V3.6.2 Thanks for your patience!
Edit1: For clarification this does not work.
mylist=ListProperty(['','','','','','',''])
for i in range(1,13):
mylist.append(calendar.month_name[i])
as
AttributeError: 'kivy.properties.ListProperty' object has no attribute 'append'
So I solved the issue with List comprehensions.
Instead of using a loop outside the variable i want to manupilate list comprehension cut down on length of the code and made updating my variables a non issue.
I realised this while reading the docs.
class Telldate(AnchorLayout):
mylist=ListProperty(['','','','','','','','','','','','',''])
print(mylist)
def __init__(self,*args, **kwargs):
super().__init__(*args, **kwargs)
self.mylist= [calendar.month_name[i][0:3] for i in range(1,13)]
Hope this helps someone someday!