KivyMD 中的 AttributeError 和 TypeError (Python)
AttributeError and TypeError in KivyMD (Python)
我在 运行 我的 KivyMD
应用程序时遇到了这个错误。我是这个领域的新手,不知道如何解决它,我对此一无所知。但我想防止这个错误。我从互联网上尝试了很多方法。但没有成功。谁能解决这个问题?请...
.kv
MDScreen:
name: "welcome"
MDFloatLayout:
md_bg_color : 1, 1, 1, 1
Carousel:
id: carousel
on_current_slide: app.current_slide(self.index)
MDFloatLayout:
Image:
source: "Assets/1.png"
pos_hint: {"center_x": .5, "center_y": .6}
MDLabel:
text: "1"
pos_hint: {"center_y": .087}
MDFloatLayout:
Image:
source: "Assets/2.png"
pos_hint: {"center_x": .5, "center_y": .6}
MDLabel:
text: "2"
pos_hint: {"center_y": .087}
.py
class DiaryApp (MDApp):
def build(self):
screen_manager = ScreenManager()
screen_manager.add_widget(Builder.load_file('kv/Diary.kv'))
return screen_manager
def on_start(self):
carousel = self.root.ids.carousel
carousel.loop = True
Clock.schedule_interval(carousel.load_next, 3.0)
def current_slide(self, index):
pass
def next(self):
self.root.ids.carousel.load_next(mode="next")
DiaryApp().run()
错误
Traceback (most recent call last):
File "kivy\properties.pyx", line 961, in kivy.properties.ObservableDict.__getattr__
KeyError: 'carousel'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\Kusal\Python\Projects\venv\Projects\Diary\Diary.py", line 47, in <module>
DiaryApp().run()
.
.
.
File "D:\Kusal\Python\Projects\venv\Projects\Diary\Diary.py", line 22, in next
self.root.ids.carousel.load_next(mode="next")
File "kivy\properties.pyx", line 964, in kivy.properties.ObservableDict.__getattr__
AttributeError: 'super' object has no attribute '__getattr__'
问题是您试图通过 root
ids
访问 carousel
id
,但是 id
是在 MDScreen
,这不是 root
。解决方法是通过定义它的 MDScreen
访问它:
def on_start(self):
welcome_screen = self.root.get_screen('welcome')
carousel = welcome_screen.ids.carousel
carousel.loop = True
Clock.schedule_interval(carousel.load_next, 3.0)
我在 运行 我的 KivyMD
应用程序时遇到了这个错误。我是这个领域的新手,不知道如何解决它,我对此一无所知。但我想防止这个错误。我从互联网上尝试了很多方法。但没有成功。谁能解决这个问题?请...
.kv
MDScreen:
name: "welcome"
MDFloatLayout:
md_bg_color : 1, 1, 1, 1
Carousel:
id: carousel
on_current_slide: app.current_slide(self.index)
MDFloatLayout:
Image:
source: "Assets/1.png"
pos_hint: {"center_x": .5, "center_y": .6}
MDLabel:
text: "1"
pos_hint: {"center_y": .087}
MDFloatLayout:
Image:
source: "Assets/2.png"
pos_hint: {"center_x": .5, "center_y": .6}
MDLabel:
text: "2"
pos_hint: {"center_y": .087}
.py
class DiaryApp (MDApp):
def build(self):
screen_manager = ScreenManager()
screen_manager.add_widget(Builder.load_file('kv/Diary.kv'))
return screen_manager
def on_start(self):
carousel = self.root.ids.carousel
carousel.loop = True
Clock.schedule_interval(carousel.load_next, 3.0)
def current_slide(self, index):
pass
def next(self):
self.root.ids.carousel.load_next(mode="next")
DiaryApp().run()
错误
Traceback (most recent call last):
File "kivy\properties.pyx", line 961, in kivy.properties.ObservableDict.__getattr__
KeyError: 'carousel'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\Kusal\Python\Projects\venv\Projects\Diary\Diary.py", line 47, in <module>
DiaryApp().run()
.
.
.
File "D:\Kusal\Python\Projects\venv\Projects\Diary\Diary.py", line 22, in next
self.root.ids.carousel.load_next(mode="next")
File "kivy\properties.pyx", line 964, in kivy.properties.ObservableDict.__getattr__
AttributeError: 'super' object has no attribute '__getattr__'
问题是您试图通过 root
ids
访问 carousel
id
,但是 id
是在 MDScreen
,这不是 root
。解决方法是通过定义它的 MDScreen
访问它:
def on_start(self):
welcome_screen = self.root.get_screen('welcome')
carousel = welcome_screen.ids.carousel
carousel.loop = True
Clock.schedule_interval(carousel.load_next, 3.0)