在Kivy中,可以通过以下步骤访问由"Add"按钮创建的TextInputs中的文本输入:
import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
class MyBoxLayout(BoxLayout):
def __init__(self, **kwargs):
super(MyBoxLayout, self).__init__(**kwargs)
self.orientation = 'vertical'
self.text_inputs = []
self.add_button = Button(text="Add")
self.add_button.bind(on_release=self.add_text_input)
self.add_widget(self.add_button)
def add_text_input(self, instance):
text_input = TextInput()
self.text_inputs.append(text_input)
self.add_widget(text_input)
class MyApp(App):
def build(self):
return MyBoxLayout()
def get_text_inputs(self):
text_inputs_text = []
for text_input in self.root.text_inputs:
text_inputs_text.append(text_input.text)
return text_inputs_text
def build(self):
layout = MyBoxLayout()
get_inputs_button = Button(text="Get Inputs")
get_inputs_button.bind(on_release=self.get_and_print_text_inputs)
layout.add_widget(get_inputs_button)
return layout
def get_and_print_text_inputs(self, instance):
text_inputs_text = self.get_text_inputs()
print(text_inputs_text)
通过以上步骤,你可以在Kivy应用中使用"Add"按钮创建TextInputs,并通过"Get Inputs"按钮获取这些TextInputs中的文本输入。
领取专属 10元无门槛券
手把手带您无忧上云