[英]Kivy - AttributeError: 'DialogContent' object has no attribute 'manager'
當我使用“取消”按鈕關閉對話框或嘗試使用“保存”按鈕添加任務時,它會創建一個屬性錯誤,指出 DialogContent 沒有名為 manager 的屬性。 我想使用 root.manager.get_screen function 從我的 RoutineCreation 屏幕中引用 close_dialog 或 add_task,我遇到了這個錯誤。
File "kivy\_event.pyx", line 727, in kivy._event.EventDispatcher.dispatch
File "kivy\_event.pyx", line 1307, in kivy._event.EventObservers.dispatch
File "kivy\_event.pyx", line 1191, in kivy._event.EventObservers._dispatch
File "C:\Users\alexa\PycharmProjects\TheFinalApp\venv\lib\site-packages\kivy\lang\builder.py", line 55, in custom_callback
exec(__kvlang__.co_value, idmap)
File "C:\Users\alexa\PycharmProjects\TheFinalApp\ardour.kv", line 91, in <module>
on_release: root.manager.get_screen('RoutineCreation').add_task(task_text, date_text.text), root.manager.get_screen('RoutineCreation').close_dialog()
File "kivy\weakproxy.pyx", line 32, in kivy.weakproxy.WeakProxy.__getattr__
AttributeError: 'DialogContent' object has no attribute 'manager'
**My Code:**
class Ardour(MDApp):
pass
class RoutineCreation(Screen):
routine_dialog = None
def show_routine_dialog(self):
if not self.routine_dialog:
self.routine_list_dialog = MDDialog(
title="Routine Creation",
type="custom",
content_cls=DialogContent(),
)
self.routine_list_dialog.open()
def close_dialog(self, *args):
self.routine_list_dialog.dismiss()
def add_task(self, task, task_date):
print(task.text, task_date)
task.text = ''
pass
class DialogContent(MDBoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.ids.date_text.text = str(datetime.now().strftime('%A %d %B %Y'))
def show_date_picker(self):
date_dialog = MDDatePicker()
date_dialog.bind(on_save = self.on_save)
date_dialog.open()
def on_save(self, instance, value, date_range):
date = value.strftime('%A %d %B %Y')
self.ids.date_text.text = str(date)
pass
**My .kv File**
<RoutineCreation>:
RectTwo:
OrangeBar:
OrangeBarTwo:
Image:
source: 'image/routinelabel.png'
size: '300dp', '100dp'
pos_hint: {'center_x': .5, 'center_y': .88}
MDList:
id: the_container
Button:
size_hint: None, None
size: '300dp', '100dp'
pos_hint: {'center_x': .5, 'center_y': .5}
on_release: root.manager.get_screen('RoutineCreation').show_routine_dialog()
background_color: 0, 0, 0, 0
Image:
source: 'image/addplan.png'
center_x: self.parent.center_x
center_y: self.parent.center_y
size: self.parent.size
Button:
size_hint: None, None
size: '200dp', '50dp'
pos_hint: {'center_x': .275, 'center_y': .125}
background_color: 0, 0, 0, 0
on_release:
app.root.current = 'MenuScreen'
Image:
source: 'image/finish.png'
center_x: self.parent.center_x
center_y: self.parent.center_y
size: self.parent.size
Button:
size_hint: None, None
size: '200dp', '50dp'
pos_hint: {'center_x': .725, 'center_y': .125}
background_color: 0, 0, 0, 0
on_release:
app.root.current = 'ChoiceScreen'
Image:
source: 'image/cancel.png'
center_x: self.parent.center_x
center_y: self.parent.center_y
size: self.parent.size
<DialogContent>:
orientation: 'vertical'
spacing: '10dp'
size_hint: 1, None
height: '130dp'
GridLayout:
rows: 1
MDTextField:
id: task_text
hint_text: 'Add Routine'
pos_hint: {'center_y': .4}
max_text_length: 40
on_text_validate: root.get_screen('RoutineCreation').add_task(task_text, date_text.text)
MDIconButton:
icon: 'calendar'
on_release: root.show_date_picker()
padding: '10dp'
MDLabel:
spacing: '10dp'
id: date_text
BoxLayout:
orientation: 'vertical'
MDRaisedButton:
text: 'Save'
on_release: root.manager.get_screen('RoutineCreation').add_task(task_text, date_text.text), root.manager.get_screen('RoutineCreation').close_dialog()
MDFlatButton:
text: 'Cancel'
on_release: root.manager.get_screen('RoutineCreation').close_dialog()
根據root
關鍵字的kv
lang 文檔:
此關鍵字僅在規則定義中可用,表示規則的根小部件(規則的第一個實例)
因此,在您的<DialogContent>:
規則中, root
指的是DialogContent
實例。
但是,相同的文檔中提到了app
關鍵字:
此關鍵字始終指代您的應用實例。
因此,您可能想要替換:
root.manager.get_screen
和:
app.root.manager.get_screen
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.