python - 如何通过.py文件上的id修改kivy小部件属性

标签 python widget kivy

我有一个标签和一个文本输入,我想通过 .py 文件上的变量更改它的文本属性。 代码如下:

这是 .kv

<DefinicaoDeSaidas>:

    BoxLayout:

        orientation:'vertical'

        BoxLayout:

            orientation:'vertical'

            padding: 0,20,20,50

            Image:

                source:'exemplo.png'

            Label:

                text:'Escolha as missoes para a saida'
                font_size:40

            TextInput:

                id: ti

这是.py

class DefinicaoDeSaidas(Screen): 

    #get the values and everything

    #transformate a variable ('name') onto the ti.text property 

您可以通过github仓库查看所有代码:https://github.com/Enzodtz/FLL-Artificial-Inteligence

最佳答案

不要在 .py 中使用 id,因为它通常会导致出现类似 foo_object.ids.ti 的代码,相反,您可以将其公开为属性:

<DefinicaoDeSaidas>:
    ti: ti # <---
    BoxLayout:
        orientation:'vertical'

        BoxLayout:
            orientation:'vertical'
            padding: 0,20,20,50

            Image:
                source:'exemplo.png'

            Label:
                text:'Escolha as missoes para a saida'
                font_size:40

            TextInput:
                id: ti

然后您可以通过任何方法使用 self 进行访问:

class DefinicaoDeSaidas(Screen): 
    # ...
    def foo_function(self, another_arguments):
        v = self.ti.text # get text
        self.ti.text = u # update text

关于python - 如何通过.py文件上的id修改kivy小部件属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54029533/

相关文章:

python - tensorflow /keras神经网络中的过拟合和数据泄漏

c - Gtk3 - 如何在它的容器中获取小部件的索引?

android - 如何使用kivy处理android运行时权限

Python:如何从 xml 获取 namespace ?

python - 可迭代对象的公共(public)基类?

python - Django 如何在请求中传递基本授权 header ?

Android UI 小部件垂直对齐问题

flutter - 导航到另一个页面 View 或任何 View 时,Bloc 小部件引用技术数据(重建)

python - 如何同时运行 kivy 和 flask 应用程序?

kivy - kivy "instance"中有什么?像 on_pressed(self, instance, pos)