[英]Python Kivy Screen Manager 'AttributeError'
我正在嘗試在屏幕之間切換,但是我的屏幕管理器失敗了,它說它的類型為NoneType。 我懷疑我可能無法正確參考我的屏幕管理器嗎? 正確的方法是什么?
我采用的方法直接來自文檔,因此我不確定我要去哪里。
錯誤:
on_release: root.manager.current = 'AboutUsWindow'
AttributeError: 'NoneType' object has no attribute 'current'
我的.kv文件:
<MainWindow>:
name: "MainWindow"
BoxLayout:
orientation: "horizontal"
ActionBar:
pos_hint: {'top':1}
use_separator: True
background_color: 0, 1, 1, 1
ActionView:
use_separator: True
ActionPrevious:
with_previous: True
ActionOverflow:
ActionButton:
# text: "some text"
text: root.name
on_release: root.manager.current = 'AboutUsWindow'
ActionButton:
text: "sponsors"
on_release: root.manager.current = 'AboutUsWindow'
ActionButton:
text: "contact"
ActionButton:
text: "event"
<AboutUsWindow>:
name: "AboutUsWindow"
Label:
text: "asdasdasd"
Button:
size: (123,123)
我的main.py文件:
# Here are imports which i did not include
class MainWindow(Screen, BoxLayout, CoverImage):
pass
class AboutUsWindow(Screen, BoxLayout, CoverImage):
pass
sm = ScreenManager()
sm.transition = FadeTransition()
sm.add_widget(MainWindow())
sm.add_widget(AboutUsWindow())
class PystokApp(App):
def build(self):
root = MainWindow(source='images/logo.jpg')
return root
# main = MainWindow()
# Window.size = main.size
# return MainWindow()
if __name__ == "__main__":
PystokApp().run()
您不使用ScreenManager。 您創建一個ScreenManager sm
並向其中添加屏幕,但是此后您不使用它,而是創建一個新的MainScreen(未鏈接到您的管理器)。 您的根Widget應該是您的屏幕管理器,這意味着您的build()
函數應該返回ScreenManager。 在文檔中,這與return(sm)
結合在一起。
另外,在創建屏幕時還需要命名屏幕。 您可以通過Screen(name="myscreen")
來執行此操作,否則您的經理將不知道名稱與哪個屏幕相對應。
您的應用可能未顯示屏幕,因為即使將它們添加到屏幕管理器中,您的PystokApp()類也不會返回屏幕管理器。
代替return root,請嘗試return sm。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.