你如何在 kivymd 中获得你当前的屏幕?
how do you get your current screen in kivymd?
所以我的主要应用程序 class 看起来像这样
class MainApp(MDApp):
def on_resize(self):
if self.app.manager.get_screen() == "table_screen":
if self.width>self.height:
self.manager.get_screen("table_screen").ids.table_image.source="table1.png"
self.manager.get_screen("table_screen").ids.left_panel.opacity=0
else:
self.ids.left_panel.size_hint_x=.5
self.ids.left_panel.opacity=1
def build(self):
Window.bind(size=self.on_resize())
return Main()
我想首先知道当前屏幕是“table_screen”,然后我需要从该屏幕获取 ID,但使用当前代码我得到一个错误
if self.root.manager.get_screen() == "table_screen":
AttributeError: 'NoneType' object has no attribute 'manager
那有什么方法呢?
这是我的主
class Main(MDBoxLayout):
pass
这是我在 kv 中的 Main
<Main>:
orientation: "vertical"
id: main
ScreenManager:
id:SM
首先,改变
Window.bind(size=self.on_resize())
到
Window.bind(size=self.on_resize)
由于 App
的根 class 包含 ScreenManager
,您可以将 on_resize()
重写为:
def on_resize(self, *args):
if self.root.ids.SM.current == "table_screen":
if self.width>self.height:
self.root.ids.SM.current_screen.ids.table_image.source="table1.png"
self.root.ids.SM.current_screen.ids.left_panel.opacity=0
else:
self.ids.left_panel.size_hint_x=.5
self.ids.left_panel.opacity=1
ScreenManager
的 current
属性 包含当前屏幕的名称,current_screen
属性 包含对当前 Screen
对象。由于您尚未发布 MCVE,因此我无法确定您对 ids
的使用是否正确。
所以我的主要应用程序 class 看起来像这样
class MainApp(MDApp):
def on_resize(self):
if self.app.manager.get_screen() == "table_screen":
if self.width>self.height:
self.manager.get_screen("table_screen").ids.table_image.source="table1.png"
self.manager.get_screen("table_screen").ids.left_panel.opacity=0
else:
self.ids.left_panel.size_hint_x=.5
self.ids.left_panel.opacity=1
def build(self):
Window.bind(size=self.on_resize())
return Main()
我想首先知道当前屏幕是“table_screen”,然后我需要从该屏幕获取 ID,但使用当前代码我得到一个错误
if self.root.manager.get_screen() == "table_screen":
AttributeError: 'NoneType' object has no attribute 'manager
那有什么方法呢? 这是我的主
class Main(MDBoxLayout):
pass
这是我在 kv 中的 Main
<Main>:
orientation: "vertical"
id: main
ScreenManager:
id:SM
首先,改变
Window.bind(size=self.on_resize())
到
Window.bind(size=self.on_resize)
由于 App
的根 class 包含 ScreenManager
,您可以将 on_resize()
重写为:
def on_resize(self, *args):
if self.root.ids.SM.current == "table_screen":
if self.width>self.height:
self.root.ids.SM.current_screen.ids.table_image.source="table1.png"
self.root.ids.SM.current_screen.ids.left_panel.opacity=0
else:
self.ids.left_panel.size_hint_x=.5
self.ids.left_panel.opacity=1
ScreenManager
的 current
属性 包含当前屏幕的名称,current_screen
属性 包含对当前 Screen
对象。由于您尚未发布 MCVE,因此我无法确定您对 ids
的使用是否正确。