如何使用kivy从另一个小部件树访问小部件?

时间:2016-02-09 09:21:06

标签: python kivy

我正在尝试使用kivy创建一个应用程序,它将文本从textinput小部件传递到一个按钮小部件,它们都有不同的小部件树,我可以这样做,因为我似乎无法在文档中找到任何内容此

我的python代码:

class Tes(App):
    pass

class Text(TextInput):
    pass

class Buton(Button):
    def on_push(self, text):
        print 'keep it up' + text


if __name__=='__main__':
    Tes().run()

我的Kivy代码:

#:kivy 1.9.0


FloatLayout

    canvas:
        Color:
            rgb: 0, 1, 0
        Rectangle:
            size: self.size
            pos: self.pos

    TextInput:
        text: 'hi'
        id: text
        size_hint: (.5, .5)
        pos_hint: {'top': .4, 'left': .4}

    Text
    Buton



<Text>
    pos_hint: {'top': 1, 'right': 1}
    size_hint: (.1, .1)
    id: text

<Buton>
    pos_hint: {'top': 1, 'left': 1}
    size_hint: (.1, .1)
    id: button
    on_press: self.on_push(root.text)

1 个答案:

答案 0 :(得分:1)

id属性仅限于规则范围。这里有id

<Text>
    pos_hint: {'top': 1, 'right': 1}
    size_hint: (.1, .1)
    id: text

什么都不做,因为规则中没有其他小部件可能会访问它。因此,将id移至上限规则:

FloatLayout
    ...
    TextInput:
        id: text
        ...
    Text:
        id: label_text
    Buton:
        id: buton
相关问题