繁体   English   中英

如何获取 Kivy App 中的 Textinputs 值,通过在 Python 3.9 脚本中使用 For 循环添加?

[英]How to get Textinputs values in a Kivy App, added by using a For cycle in a Python 3.9 script?

我在 Python 3.9 脚本中有一个 Kivy 应用程序。 我使用 For 循环添加了许多标签和文本输入,所以我没有文本输入的名称。 有没有办法扫描我所有的应用程序,并获取 Textinputs 值? Ps:我已经尝试过使用 Screenshot 和 Pytesseract,但我没有得到所有值。 操作系统 Windows 10. 谢谢

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
class app(App):
    def build(self):
        box=BoxLayout(orientation='vertical')
        self.labels=['label1: ','label2: ','label3: ','label4: ','label5: ']4
        self.values=['40','30','25','180','1500']
        grid=GridLayout(cols=2)
        c=0
        for text in self.labels:
            grid.add_widget(Label(text=text)
            grid.add_widget(TextInput(text=self.values[c]))
            c+=1
        box.add_widget(grid)
        button=Button(text='GET') #updating values in textinputs,before clicking button
        button.bind(on_press=self.get_new_values)
        box.add_widget(button())
        def get_new_values(self,instance):
            print('New values: '+self.values)

在循环中创建 TextInputs 之前,创建一个空列表。

_my_list = []

在循环内部,append 每个到这个列表。

textinput = TextInput(text='Hello world')
_my_list.append(textinput)
your_widget.append(textinput)

然后你可以像这样引用它们

for one in _my_list:
    print(one.text)  # this prints the text value of one TextInput

或者可以创建一个空字典并使用唯一键将每个项目添加到字典中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM