Python 接触儿童的子女';KIvy的身份证
抱歉,如果图像太大 嗯。所以它是中等大小的,所以它只是作为参考——不需要理解代码 因此,如果我想在Python中访问布局中的Button()、Label()或TextInput()对象,我只需执行self.ids.object\u name.property\u of_object 但是,假设我有一个ScreenManager(),在ScreenManager()中有一个Screen(),在Screen对象中有一个自定义布局MyCustomLayout()。就我能够得到的结果而言——我无法从Screen()的Python代码中从MyCustomLayout()中获得ID 例如,假设在MyCustomLayout()中有一个按钮的id,my_按钮。我想更改文本 如果我在MyCustomLayout()类下,这将起作用:Python 接触儿童的子女';KIvy的身份证,python,kivy,Python,Kivy,抱歉,如果图像太大 嗯。所以它是中等大小的,所以它只是作为参考——不需要理解代码 因此,如果我想在Python中访问布局中的Button()、Label()或TextInput()对象,我只需执行self.ids.object\u name.property\u of_object 但是,假设我有一个ScreenManager(),在ScreenManager()中有一个Screen(),在Screen对象中有一个自定义布局MyCustomLayout()。就我能够得到的结果而言——我无法从S
self.ids.my_button.text = 'My new text!'
但假设我在MyScreen()中,其中包含MyCustomLayout()。我无法获得:
self.ids.my_button.text doesn't work
self.ids.my_custom_layout.my_button.text doesn't work
实际上,self.ids返回{}。出于某种原因,它甚至没有填充可观察的法令
但是,无论如何。我想我说的是这个。如果我想访问自定义小部件的子部件:
额外学分:告诉我你是怎么知道这件事的 您可以在kvlang中为对象指定一个id<代码>id:mybutton
在下面的示例中,我在第一个屏幕输入时将按钮的文本设置为随机数。
在第二个屏幕中,我只需在enter键上打印该屏幕及其子屏幕中的所有ID
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from random import choice
Builder.load_string('''
<MyCustomLayout1>:
Button:
id: mybutton
text: "Goto 2"
on_release: app.sm.current = "screen2"
<MyCustomLayout2>:
Button:
id: mybutton
text: "Goto 1"
on_release: app.sm.current = "screen1"
<Screen1>:
MyCustomLayout1:
id: mylayout
<Screen2>:
MyCustomLayout2:
id: mylayout
''')
class Screen1(Screen):
def on_enter(self,*args):
self.ids.mylayout.ids.mybutton.text = str(choice(range(100)))
class Screen2(Screen):
def on_enter(self,*args):
print(self.ids)
print(self.ids.mylayout.ids)
class MyCustomLayout1(BoxLayout):
pass
class MyCustomLayout2(BoxLayout):
pass
class MyApp(App):
def build(self):
self.sm = ScreenManager()
self.sm.add_widget(Screen1(name='screen1'))
self.sm.add_widget(Screen2(name='screen2'))
return self.sm
MyApp().run()
从kivy.app导入应用
从kivy.uix.screenmanager导入screenmanager,屏幕
从kivy.lang导入生成器
从kivy.uix.boxlayout导入boxlayout
从随机进口选择
Builder.load_字符串(“”)
:
按钮:
id:mybutton
正文:“转到2”
发布时:app.sm.current=“screen2”
:
按钮:
id:mybutton
正文:“转到1”
发布时:app.sm.current=“screen1”
:
MyCustomLayout1:
id:mylayout
:
MyCustomLayout2:
id:mylayout
''')
类别屏幕1(屏幕):
def on_enter(自身,*参数):
self.ids.mylayout.ids.mybutton.text=str(选项(范围(100)))
第2类屏幕(屏幕):
def on_enter(自身,*参数):
打印(self.id)
打印(self.ids.mylayout.ids)
类MyCustomLayout1(BoxLayout):
通过
类别MyCustomLayout2(BoxLayout):
通过
类别MyApp(应用程序):
def生成(自):
self.sm=ScreenManager()
self.sm.add_小部件(Screen1(name='Screen1'))
self.sm.add_小部件(Screen2(name='Screen2'))
返回self.sm
MyApp().run()
嵌套对象时,只需执行obj.ids.obj.ids
等操作。即使这不是问题的一部分,我可以提到,对于从boxlayout访问screenmanager,将screenmanager作为App类的一个属性是一个好主意。这样,您就可以在kvlang中以
app.sm
在结束问题时,你可以问我是从哪里学到这一点的。好吧,你做得很对,你只是没有做对筑巢。我从kivy的api参考中了解了kvlang,但我不记得它是否说明了很多嵌套。但希望在本例中,这似乎是一种合乎逻辑的方法。啊,我很高兴你这样回答。让我有机会更具体地说明我所询问的内容。让我们使用您的代码,但修改BoxLayout部分,如下所示--Gist:As is,在屏幕中实例化自定义类会为“mybutton”生成一个KeyError。如果我去掉第40-41行,我会得到一个AttributeError--“MyCustomLayout1”没有属性“manager”(它没有)。所以,问题是,我们如何使这个新代码——使用自定义类——像您的原始代码一样工作?@crowbarProgramming ahh:)我将接受一个look@crowbarProgramming更新了代码。将更新一点答案文本,这是史诗般的。我感觉很放松,感觉我现在和Kivy在一起有了更大的控制感。谢谢,这是我应得的