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

2024-09-24 22:26:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个标签和一个文本输入,我想通过.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


Tags: 文件the代码textpy文本githubti
1条回答
网友
1楼 · 发布于 2024-09-24 22:26:30

不要在.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

相关问题 更多 >