Kivy开发报错:'super'无__getattr__属性及UI异常求助
问题求助:KivyMD应用报错及UI异常
核心错误信息
主要错误如下:
line 153, in init
self.ids.date_text.text = str(datetime.now().strftime('%A %d %B %Y'))
File "kivy\properties.pyx", line 964, in kivy.properties.ObservableDict.getattr
AttributeError: 'super' object has no attribute 'getattr'
添加代码后程序无法运行,以下是相关代码文件:
main.py 相关代码
task_list_dialog = None # Here # Add the below functions def show_task_dialog(self): if not self.task_list_dialog: self.task_list_dialog = MDDialog( title="Create Task", type="custom", content_cls=DialogContent(), ) self.task_list_dialog.open() # add this entire function def on_start(self): """Load the saved tasks and add them to the MDList widget when the application starts""" try: completed_tasks, uncomplete_tasks = db.get_tasks() if uncomplete_tasks != []: for task in uncomplete_tasks: add_task = ListItemWithCheckbox(pk=task[0], text=task[1], secondary_text=task[2]) self.root.ids.container.add_widget(add_task) if completed_tasks != []: for task in completed_tasks: add_task = ListItemWithCheckbox(pk=task[0], text='[s]' + task[1] + '[/s]', secondary_text=task[2]) add_task.ids.check.active = True self.root.ids.container.add_widget(add_task) except Exception as e: print(e) pass def close_dialog(self, *args): self.task_list_dialog.dismiss() def add_task(self, task, task_date): '''Add task to the list of tasks''' # Add task to the db created_task = db.create_task(task.text, task_date) # Here # return the created task details and create a list item self.root.get_screen('tasks').ids.container.add_widget( ListItemWithCheckbox(pk=created_task[0], text='[b]' + created_task[1] + '[/b]', secondary_text=created_task[2])) # Here task.text = ''
class DialogContent(MDBoxLayout): def __init__(self, **kwargs): super().__init__(**kwargs) # set the date_text label to today's date when user first opens dialog box self.ids.date_text.text = str(datetime.now().strftime('%A %d %B %Y')) def show_date_picker(self): """Opens the date picker""" date_dialog = MDDatePicker() date_dialog.bind(on_save=self.on_save) date_dialog.open() def on_save(self, instance, value, date_range): """This functions gets the date from the date picker and converts its it a more friendly form then changes the date label on the dialog to that""" date = value.strftime('%A %d %B %Y') self.ids.date_text.text = str(date)
class ListItemWithCheckbox(TwoLineAvatarIconListItem): '''Custom list item''' def __init__(self, pk=None, **kwargs): super().__init__(**kwargs) # state a pk which we shall use link the list items with the database primary keys self.pk = pk def mark(self, check, the_list_item): '''mark the task as complete or incomplete''' if check.active == True: # add strikethrough to the text if the checkbox is active the_list_item.text = '[s]' + the_list_item.text + '[/s]' db.mark_task_as_complete(the_list_item.pk) # here else: the_list_item.text = str(db.mark_task_as_incomplete(the_list_item.pk)) # Here def delete_item(self, the_list_item): '''Delete the task''' self.parent.remove_widget(the_list_item) db.delete_task(the_list_item.pk) # Here
class LeftCheckbox(ILeftBodyTouch, MDCheckbox): """Custom left container"""
.kv 文件代码
MDScreen: name: "tasks" BoxLayout: orientation:'vertical' MDBottomNavigation: MDBottomNavigationItem: name: 'screen 1' icon: 'home-variant-outline' MDLabel: text: 'Goals in Progress' font_name: 'Times' font_size: '25sp' pos_hint: {"center_x": 0.75, "center_y": .970} MDIconButton: icon: "square-edit-outline" pos_hint: {"x": .869, "y": .001} elevation: 18 DialogContent: orientation: "vertical" spacing: "10dp" size_hint: 1, None height: "130dp" GridLayout: rows: 1 MDTextField: id: task_text hint_text: "Add Task..." pos_hint: {"center_y": .4} max_text_length: 50 on_text_validate: (app.add_task(task_text, date_text.text), app.close_dialog()) MDIconButton: icon: 'calendar' on_release: root.show_date_picker() padding: '10dp' MDLabel: spacing: '10dp' id: date_text BoxLayout: orientation: 'horizontal' MDRaisedButton: text: "SAVE" on_release: (app.add_task(task_text, date_text.text), app.close_dialog()) MDFlatButton: text: 'CANCEL' on_release: app.close_dialog() ListItemWithCheckbox: id: the_list_item markup: True LeftCheckbox: id: check on_release: root.mark(check, the_list_item) IconRightWidget: icon: 'trash-can-outline' theme_text_color: "Custom" text_color: 1, 0, 0, 1 on_release: root.delete_item(the_list_item)
另外,注释掉报错代码后运行,会出现异常UI行为:对话框内容直接显示在主界面而非弹窗中。恳请帮忙解决这两个问题,谢谢!
内容的提问来源于stack exchange,提问作者Abylaikhan Zhumanov
相关产品推荐
相关产品推荐

