我正在尝试创建一个屏幕,用户可以在其中输入3个MDTextField小部件,然后单击Submit在MDList中创建一个新项目(即基本任务跟踪器!)。现在,我可以使用以下代码提取为每个条目创建ThreeLineIconListItem。
.py
class TasksScreen(Screen) :
def get_date(self, date) :
'''
:type date: <class 'datetime.date'>
'''
self.ids.wiki_dateselected.text = str(date)
def show_date_picker(self) :
date_dialog = MDDatePicker(callback=self.get_date)
date_dialog.open()
def create_task(self):
task_title = self.ids.i_wiki_tasktitle.text
task_detail = self.ids.i_wiki_taskdetail.text
task_deadline = self.ids.wiki_dateselected.text
print('Task title is {}, task detail is {}, and task deadline is {}'.format(task_title, task_detail, task_deadline))
self.ids.wiki_cardcontainer.add_widget(ThreeLineIconListItem(id = 'wikitask_{}'.format(task_title),
text=task_title,
secondary_text = task_detail,
tertiary_text = task_deadline))
.kv
<TaskCard>
orientation: 'vertical'
spacing: '5dp'
MDLabel:
id: wiki_cardtitle
MDLabel:
id: wiki_carddetail
MDLabel:
id: wiki_carddate
<TasksScreen>:
name: 'TasksScreen'
MDBoxLayout:
orientation: 'horizontal'
size_hint_y: 1
MDBoxLayout:
size_hint_x: 1
orientation: 'vertical'
ScrollView:
id: note_scroll
size_hint_x: .4
MDCard:
id: wiki_card_1
orientation: 'vertical'
spacing: 10
MDLabel:
size_hint_y: None
text: 'Task Entry'
font_size: 40
halign: 'left'
md_bg_color: app.theme_cls.accent_light
elevation: 10
MDLabel:
size_hint_y: None
text: 'Task Details'
font_size: 15
halign: 'left'
MDTextField:
id: i_wiki_tasktitle
size_hint_y: None
hint_text: "Enter task title: "
mode: "rectangle"
multiline: False
required: True
color_mode: 'accent'
icon_right: "arrow-down-drop-circle-outline"
icon_right_color: app.theme_cls.accent_color
MDTextField:
id: i_wiki_taskdetail
size_hint_y: None
hint_text: "Details of task: (max 200 chars.)"
mode: "rectangle"
multiline: True
max_text_length: 200
color_mode: 'accent'
icon_right: "arrow-down-drop-circle-outline"
icon_right_color: app.theme_cls.accent_color
MDBoxLayout:
orientation: 'horizontal'
size_hint: (None, None)
height: self.minimum_height
width: root.ids.wiki_card_1.width
spacing: '20dp'
MDRaisedButton:
text: 'Select Deadline'
size_hint: (None, None)
halign: 'left'
height: '40dp'
md_bg_color: app.theme_cls.accent_light
on_release: root.show_date_picker()
MDLabel:
text: '[select date]'
id: wiki_dateselected
icon: 'calendar'
size_hint: (None, None)
height: '40dp'
width: '200dp'
MDLabel:
text: ''
size_hint_x: '0.4'
MDRaisedButton:
text: 'Submit'
md_bg_color: app.theme_cls.accent_light
on_release: root.create_task()
MDLabel:
text: 'FILLER'
elevation: 10
size_hint: (None, 1)
ScrollView:
size_hint_x: .60
MDList:
id: wiki_cardcontainer
我现在想添加从应用程序的先前实例中加载先前创建的任务的功能,即我已经创建了一些任务,关闭了应用程序,现在再次打开了应用程序,并照原样重新创建了列表。我的初学者水平知识认为使用Kivy的JsonStore模块将是这样做的一种方式(如果我缺少更好的解决方案,请纠正我!)
在我看来,我想按照下面的代码片段进行操作,但是它返回一个NameError,指出““”“名称'task_detail'未定义”“”“
TaskStore = JsonStore("TaskStore.json")
class TasksScreen(Screen) :
def on_enter(self, *args):
TaskStore.put('EmptyCard', task_detail='Insert details here', task_deadline='Insert deadline here')
for key in TaskStore.keys():
self.ids.wiki_cardcontainer.add_widget(ThreeLineIconListItem(id='wikitask {}',
text=str(TaskStore.get(key)),
secondary_text=str(TaskStore.get(key)[task_detail]),
tertiary_text=str(TaskStore.get(key)[task_deadline])))
任何有关如何使此工作正常运行的建议将不胜感激!