Python-2.7 Kivy实现:回车触发动态添加行并聚焦新行首列
Alright, let's fix this up for you! The key here is leveraging Kivy's TextInput event system to trigger adding a new row when Enter is pressed in the last input of any row, then shifting focus to the first input of the new row. Here's a complete, working implementation based on your code snippet:
Modified Code Implementation
from kivy.uix.screenmanager import Screen from kivy.app import App from kivy.lang import Builder from kivy.core.window import Window from kivy.uix.boxlayout import BoxLayout from kivy.properties import StringProperty from kivy.clock import Clock # Custom row layout holding your TextInputs class RowLayout(BoxLayout): col1_text = StringProperty("") col2_text = StringProperty("") col3_text = StringProperty("") # This is our "last TextBox" per row def __init__(self, **kwargs): super(RowLayout, self).__init__(**kwargs) # Bind Enter press on the last input to trigger new row addition self.ids.last_input.bind(on_text_validate=self.add_new_row) def add_new_row(self, instance): # Get the parent container that holds all rows (adjust hierarchy if needed) row_container = self.parent # Create and add a new row new_row = RowLayout() row_container.add_widget(new_row) # Delay focus to ensure the new row finishes rendering first Clock.schedule_once(lambda dt: new_row.ids.first_input.focus = True) class MainScreen(Screen): def add_row_manually(self): # Reuse the same logic as auto-add for consistency new_row = RowLayout() self.ids.rows_container.add_widget(new_row) Clock.schedule_once(lambda dt: new_row.ids.first_input.focus = True) class TestApp(App): def build(self): Window.clearcolor = (1, 1, 1, 1) return Builder.load_string(""" <RowLayout>: orientation: 'horizontal' spacing: 10 size_hint_y: None height: 40 TextInput: id: first_input text: root.col1_text size_hint_x: 0.3 TextInput: text: root.col2_text size_hint_x: 0.3 TextInput: id: last_input text: root.col3_text size_hint_x: 0.3 multiline: False # Critical: Makes Enter trigger validation instead of new line <MainScreen>: BoxLayout: orientation: 'vertical' padding: 20 spacing: 15 BoxLayout: id: rows_container orientation: 'vertical' spacing: 10 # Start with one initial row RowLayout: Button: text: "+Add More" size_hint_y: None height: 40 on_press: root.add_row_manually() """) if __name__ == "__main__": TestApp().run()
Key Details Explained
on_text_validateEvent: Kivy'sTextInputfires this event when the user presses Enter (only ifmultilineis set toFalse). We bind this event on the last input of each row to trigger theadd_new_rowmethod.- Focus Management: We use
Clock.schedule_onceto delay setting focus on the new row's first input. This ensures the new row has finished rendering before we try to focus it—without this delay, the focus might not work as expected. - Consistent Logic: The manual "+Add More" button reuses the same row-creation and focus logic as the auto-add feature, so both behaviors are identical.
内容的提问来源于stack exchange,提问作者Nirdesh Kumawat
相关产品推荐
相关产品推荐

