Kivy动态控件问题

2024-10-01 19:29:50 发布

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

嗯,我是一个使用kivy框架的初学者,所以我想这里有人可以帮我。 我的问题是: 在我的应用程序中,用户输入一个数字n,然后应用程序返回n个TextInput小部件。但是如何使用每个TextInput上插入的值呢?第一部分很容易做到,我是按单子做的。(如果有人知道如何直接在kv文件上做,我会很感激的)。我的问题是第二部分,我需要稍后使用和操作这些值(在TextInputs中),但我无法访问它们。我的意思是,我为列表中的每个小部件设置了一个id,但是我无法访问它们的.text属性。下面是我的一段代码:

class FirstLayout(BoxLayout):

   def next_layout(self):  
   self.clear_widgets()  
   secondLayout = Factory.SecondLayout()  
   number = NumericProperty()  
# This 'number' variable came from another widget on kv file

   textinput_list = []
# That list will receive as many TextInputs field as my variable 'number' said (by the loop for)

   for i in range(int(self.number.text)):
        textinput_list.append(TextInput())
        textinput_list[i].id = "input" + str(i + 1)
    # So for each textinput, i added a id named 'inputx' (where x is the number of the current
    # iteration) my question resides here. How could i use those values (inside of each textinput 
    # on this list
    # later? Because i'm not creating these widgets (TextInputs) on kv language so i don't know how to
    # bind the ids for a new variable directly in .py file
        secondLayout.container.add_widget(textinput_list[i]) 

    self.add_widget(secondLayout)

Tags: theselfid应用程序numberforontextinput
1条回答
网友
1楼 · 发布于 2024-10-01 19:29:50

如果我没弄错你的问题,你只需要把textinput_list作为一个类变量。所以这个。在

textinput_list = []

变成

^{pr2}$

假设有一个名为FirstLayout的对象first。使用first.textinput_list[0]可以访问第一个textinput,依此类推。在

如果您想通过id轻松访问textinputs,我建议您使用字典,键是id的,值是输入。在

相关问题 更多 >

    热门问题