Kvlang无法从动态窗口小部件类KeyError中找到ID

时间:2019-12-17 05:11:37

标签: python kivy

所以我有一个像这样的动态kvlang文件

<LeftSideButtonPanel@BoxLayout>:
    orientation: 'vertical'
    size_hint_min: 200, 20
    size_hint_max: 300, 20
    Label:
        id: nickname
        text: "NICKNAME_NOT_FOUND"
        font_size: 18

<Connected>:
    BoxLayout:
        orientation: 'horizontal'
        LeftSideButtonPanel:
        Label:
            id: another_name
            text: "EMPTY"

和main.py

import kivy
kivy.require('1.11.1')
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, Builder

Builder.load_file("screens.kv")
class Connected(Screen):
    def on_enter(self):
        self.ids['nickname'].text = "NICKNAMEGOESHERE"
        self.ids['another_name'].text = "ANOTHERNAMEHERE"


class MyApp(App):

    def build(self):
        sm = ScreenManager()

        sm.add_widget(Connected(name="connected"))

        return sm

if __name__ == "__main__":
    MyApp().run()

我要插入另一个BoxLayout中,并且一旦用户通过身份验证,便试图更改昵称字段,但是由于某种原因,我找不到带有.ids['nickname']的Label。给出错误KeyError: 'nickname',但是如果我要在另一个BoxLayout内部手动创建Label,我将在示例中找到它another_name。从on_enter事件处理程序中调用它。

错误消息:

Traceback (most recent call last):
   File "c:/Users/Ranta/Projects/Python/testing/KivyTesting/main.py", line 24, in <module>
     MyApp().run()
   File "C:\Users\Ranta\Projects\Python\testing\KivyTesting\kivy_venv\lib\site-packages\kivy\app.py", line 829, in run
     root = self.build()
   File "c:/Users/Ranta/Projects/Python/testing/KivyTesting/main.py", line 19, in build
     sm.add_widget(Connected(name="connected"))
   File "C:\Users\Ranta\Projects\Python\testing\KivyTesting\kivy_venv\lib\site-packages\kivy\uix\screenmanager.py", line 992, in add_widget
     self.current = screen.name
   File "kivy\properties.pyx", line 497, in kivy.properties.Property.__set__
   File "kivy\properties.pyx", line 544, in kivy.properties.Property.set
   File "kivy\properties.pyx", line 599, in kivy.properties.Property.dispatch
   File "C:\Users\Ranta\Projects\Python\testing\KivyTesting\kivy_venv\lib\site-packages\kivy\uix\screenmanager.py", line 1055, in on_current
     screen.dispatch('on_enter')
   File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
   File "c:/Users/Ranta/Projects/Python/testing/KivyTesting/main.py", line 11, in on_enter
     self.ids['nickname'].text = "this"
 KeyError: 'nickname'

1 个答案:

答案 0 :(得分:0)

id是通过root访问的,在您的情况下,例如,“ another_name”植根于“ Connected”,因此使用self.ids['another_name'].text =" ANOTHERNAMEHERE "是有效的,因为self是“ Connected”的实例,而“ nickname”并非植根于“已连接”,但已连接到“ LeftSideButtonPanel”,因此您必须首先访问该实例,并为此设置一个ID:

# ...
<Connected>:
    BoxLayout:
        orientation: 'horizontal'
        LeftSideButtonPanel:
            id: left_panel
        Label:
            id: another_name
            text: "EMPTY"
class Connected(Screen):
    def on_enter(self):
        self.ids["left_panel"].ids["nickname"].text = "NICKNAMEGOESHERE"
        # or
        # self.ids.left_panel.ids.nickname.text = "NICKNAMEGOESHERE"
        self.ids["another_name"].text = "ANOTHERNAMEHERE"