我想讓我的頭繞過屏幕管理器,特別是引用其中的對象。引用屏幕管理器對象中的ID
我用它來設置一個值:
class Widgets(Widget)
pass
w = Widgets()
w.ids.MyTitle.text = 'something'
現在我有這樣的:
class Widgets(Screen)
pass
class SettingsScreen(Screen)
pass
sm = ScreenManager()
sm.add_widget(Widgets(name='main'))
sm.add_widget(SettingsScreen(name='settings'))
我如何引用MyTitle現在?我試過各種組合,如:
sm.ids.main.MyTitle.text =
sm.main.MyTitle.text =
sm.main.ids.MyTitle.text =
....但沒有得到它!有人能讓我擺脫苦難嗎?有沒有一種簡單的方法瀏覽sm對象或者遍歷它?
編輯:添加最小的運行版本:
minimal.kv:
# File name: minimal.py
#:kivy 1.8.0
<Widgets>
Button:
id: MyTitle
text: 'hello'
<SettingsScreen>:
Button:
id: Other
text: 'settings'
minimal.py:
from kivy.uix.widget import Widget
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.button import Button
from kivy.lang import Builder
from kivy.app import App
class Widgets(Screen):
pass
class SettingsScreen(Screen):
pass
class myApp(App):
def build(self):
return sm
def on_start(self):
global sm
sm.ids['main'].ids['MyTitle'].text = 'changed' # <-- this fails
Builder.load_file("minimal.kv")
sm = ScreenManager()
sm.add_widget(Widgets(name='main'))
sm.add_widget(SettingsScreen(name='settings'))
if __name__ == '__main__':
myApp().run()
你可以加個[mcve]嗎?因爲你的代碼不會運行。它也是定義ID的地方。 – zeeMonkeez