我试着了解一下screenmanager,特别是引用其中的对象。在
我用这个来设置一个值:
class Widgets(Widget)
pass
w = Widgets()
w.ids.MyTitle.text = 'something'
现在我有了这个:
^{pr2}$我现在如何引用我的标题?我试过各种组合,比如:
sm.ids.main.MyTitle.text =
sm.main.MyTitle.text =
sm.main.ids.MyTitle.text =
。。。。但没有得到它!有人能让我摆脱痛苦吗?有没有一种简单的方法可以浏览sm对象或者迭代它?在
编辑:添加最小运行版本:
在最小.kv公司名称:
# File name: minimal.py
#:kivy 1.8.0
<Widgets>
Button:
id: MyTitle
text: 'hello'
<SettingsScreen>:
Button:
id: Other
text: 'settings'
在最小.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()
根据the documentation,您可以访问
id
,就像访问任何字典键一样:因为^{} 本身是从
^{pr2}$Widget
派生的,并且给定的小部件维护a list of widgets it is aware of,所以您可能需要类似以下内容:然而,如果没有Minimal, Complete, and Verifiable Example,这很难说。你可以做的一件事是:
作为旁注,这:
。。。可能会引起混淆,因为您使用}。OOP建议类的子类应该是类的更具体的形式。因此,
Widgets
(通过中间类Screen
)扩展{Screen
是Widget
的类型。但是Widgets
实际上是Widgets
的一个数。在要从} :
ScreenManager
获取屏幕,请使用^{此外,您可以构建应用程序,以便: kv文件:
^{pr2}$在python文件中:
相关问题 更多 >
编程相关推荐