簡體   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