如何在Kivy中设置动态TextInput的ID [英] How to set ID of dynamic TextInput in Kivy

查看:87
本文介绍了如何在Kivy中设置动态TextInput的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个很简单的问题,我可能就是不懂生活,

Very simple question I might just not be comprehending life,

self.ids.UpdateCustomer1.add_widget(TextInput(text='123',id=str('AddUserTextBox')))

这是我的代码,我只想设置按下按钮时弹出的动态文本框的ID.一切正常,但是每当我引用AddUserTextBox.text时,它就会告诉我它不存在.或更具体的是"NameError:名称'AddUserTextBox'未定义"

Here is my code, I just want to set the id of a dynamic text box that pops up when a button is pressed. that all works fine, but whenever I reference AddUserTextBox.text it tells me its non-existent. or more specifically " NameError: name 'AddUserTextBox' is not defined"

现在我已经尝试将id ='AddUserTextBox'设置为id = AddUserTextBox,但我没有运气.所有帮助将不胜感激

Now I have tried setting id='AddUserTextBox' and also as id=AddUserTextBox and I have had no luck. all help would be greatly appreciated

推荐答案

在第一次处理您的 .kv 文件(或字符串)时未设置 ids 之后更新.因此,新的 TextInput 小部件ID不会添加到 ids 中.您可以通过以下方法解决此问题:

The ids are setup when your .kv file (or string) is first processed and is not updated after that. So your new TextInput widget id does not get added to the ids. You can work around that by doing:

import weakref
textinput = TextInput(text='123')
self.ids.UpdateCustomer1.add_widget(textinput)
self.ids['AddUserTextBox'] = weakref.ref(textinput)

之所以使用 weakref ,是因为它是kivy最初设置 ids 字典时所使用的.这不是最好的方法.最好保留对您添加的 TextInput 的引用.

The weakref is used because that is what kivy uses when it initially sets up the ids dictionary. This is not the best way to do it. It would be better just to keep a reference to your added TextInput.

这篇关于如何在Kivy中设置动态TextInput的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆