【问题标题】:How can I access a widget from another widget tree using kivy?如何使用 kivy 从另一个小部件树访问小部件?
【发布时间】:2016-02-09 09:21:05
【问题描述】:

我正在尝试使用 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)

【问题讨论】:

    标签: python kivy


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-30
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多