Kivy技术问题:如何将Name TextInput文本传递至Filter TextInput实现过滤?
Solution for Syncing Name Input to Filter and Filtering TreeView in Kivy
Alright, let's get this Kivy setup working exactly how you need it. The core idea is to sync the text from your 'Name' input directly to the 'Filter' input, then use that text to filter the TreeView automatically once it's visible. Here's a complete, working implementation:
Step-by-Step Implementation
First, we'll create a Screen class that handles the input sync, TreeView visibility, and filtering logic. Then we'll wrap it in an App instance.
Full Code Example
from kivy.app import App from kivy.uix.screenmanager import Screen from kivy.uix.boxlayout import BoxLayout from kivy.uix.textinput import TextInput from kivy.uix.treeview import TreeView, TreeViewLabel from kivy.properties import StringProperty class MainScreen(Screen): filter_text = StringProperty("") def __init__(self, **kwargs): super().__init__(**kwargs) # Main vertical layout main_layout = BoxLayout(orientation='vertical', padding=10, spacing=10) # Name TextInput setup self.name_input = TextInput(hint_text="Enter Name", multiline=False) # Bind text changes to sync filter and toggle TreeView visibility self.name_input.bind(text=self.sync_filter_text) main_layout.add_widget(self.name_input) # Filter + TreeView container (hidden by default) self.filter_tree_container = BoxLayout(orientation='vertical', spacing=10, opacity=0, size_hint_y=None) # Filter TextInput setup self.filter_input = TextInput(hint_text="Filter TreeView", multiline=False) # Bind filter text changes to the TreeView filter function self.filter_input.bind(text=self.filter_treeview) self.filter_tree_container.add_widget(self.filter_input) # Initialize TreeView with sample data self.tree_view = TreeView(root_options={'text': 'Root'}) self.populate_sample_nodes() self.filter_tree_container.add_widget(self.tree_view) main_layout.add_widget(self.filter_tree_container) self.add_widget(main_layout) def sync_filter_text(self, input_instance, new_text): # Sync Name input text to Filter input self.filter_input.text = new_text # Show/hide the Filter + TreeView container based on input content if new_text.strip(): self.filter_tree_container.opacity = 1 self.filter_tree_container.size_hint_y = 1 else: self.filter_tree_container.opacity = 0 self.filter_tree_container.size_hint_y = None # Clear filter when Name input is empty self.filter_treeview(None, "") def populate_sample_nodes(self): # Add sample nodes (replace this with your actual data) root = self.tree_view.root for category in ["Customers", "Products", "Orders"]: category_node = TreeViewLabel(text=category) self.tree_view.add_node(category_node, root) for i in range(3): item_node = TreeViewLabel(text=f"{category} - {category[:3]}{i+1}") self.tree_view.add_node(item_node, category_node) def filter_treeview(self, filter_instance, filter_keyword): # Filter logic: show nodes that contain the keyword (case-insensitive) keyword = filter_keyword.lower().strip() # Iterate all nodes and toggle visibility for node in self.tree_view.iterate_all_nodes(): if node == self.tree_view.root: continue # Skip root node # Check if node text matches the keyword if keyword in node.text.lower(): node.opacity = 1 # Ensure all parent nodes are visible so the child can be seen parent = node.parent_node while parent != self.tree_view.root: parent.opacity = 1 parent = parent.parent_node else: node.opacity = 0 class TestApp(App): def build(self): return MainScreen() if __name__ == "__main__": TestApp().run()
Key Features Explained
- Text Sync: The
sync_filter_textmethod is bound to the Name input'stextproperty. Every time you type in the Name input, it automatically updates the Filter input's text. - Dynamic Visibility: The Filter + TreeView container starts hidden. When you enter text in the Name input, it becomes visible; when you clear the Name input, it hides again.
- Smart Filtering: The
filter_treeviewmethod checks each node's text against the synced keyword. It also ensures parent nodes stay visible if any of their children match the filter, so you don't miss matching items nested deep in the tree.
How to Use
- Save this code as
test.py - Run it with Kivy installed (
python test.py) - Type any text in the "Enter Name" input: the Filter input will auto-fill with the same text, and the TreeView will filter to show matching nodes immediately.
内容的提问来源于stack exchange,提问作者macson taylor
相关产品推荐
相关产品推荐

