import sqlite3 as lite
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.popup import Popup
from kivy.uix.treeview import TreeView, TreeViewLabel, TreeViewNode
from kivy.uix.label import Label
from kivy.properties import ObjectProperty
Window.clearcolor = (0, 0.517, 0.705, 1)
Window.size = (700, 530)
con = lite.connect('fact.db')
con.text_factory = str
cur = con.cursor()
def populate_tree_view(tree_view, parent, node):
if parent is None:
tree_node = tree_view.add_node(TreeViewLabel(text=node['node_id'],
is_open=True))
else:
tree_node = tree_view.add_node(TreeViewLabel(text=node['node_id'],
is_open=True), parent)
for child_node in node['children']:
populate_tree_view(tree_view, tree_node, child_node)
rows = [(1, 'test1', 111), (2, 'test2', 112), (3, 'test3', 113), (4, 'test4', 114)]
tree = [{'node_id': 'Test2',
'children': []},
{'node_id': 'Test3',
'children': []}]
class TreeViewLabel(Label, TreeViewNode):
pass
class TreeviewGroup(Popup):
treeview = ObjectProperty(None)
tv = ObjectProperty(None)
def __init__(self, **kwargs):
super(TreeviewGroup, self).__init__(**kwargs)
self.tv = TreeView(root_options=dict(text="Test1"),
hide_root=False,
indent_level=4)
for branch in tree:
populate_tree_view(self.tv, None, branch)
self.remove_widgets()
self.treeview.add_widget(self.tv)
def remove_widgets(self):
for child in [child for child in self.treeview.children]:
self.treeview.remove_widget(child)
class GroupScreen(Screen):
groupName = ObjectProperty(None)
popup = ObjectProperty(None)
def display_groups(self, instance):
if len(instance.text) > 0:
self.popup = TreeviewGroup()
self.popup.open()
class Group(App):
rows = [(1, 'test1', 111), (2, 'test2', 112), (3, 'test3', 113), (4, 'test4', 114)]
def build(self):
self.root = Builder.load_file('test.kv')
return self.root
if __name__ == '__main__':
Group().run()
<TreeViewLabel>:
on_touch_down:
app.root.stateName.text = self.text
app.root.popup.dismiss()
<TreeviewGroup>:
id: treeview
treeview: treeview
title: "Select City"
size_hint: None, None
size: 400, 400
auto_dismiss: False
BoxLayout
orientation: "vertical"
BoxLayout:
id: treeview
Button:
size_hint: 1, 0.1
text: "Close"
on_release: root.dismiss()
<CustomLabel@Label>:
text_size: self.size
valign: "middle"
padding_x: 5
<SingleLineTextInput@TextInput>:
multiline: False
<GreenButton@Button>:
background_color: 1, 1, 1, 1
size_hint_y: None
height: self.parent.height * 0.150
GroupScreen:
stateName: stateName
GridLayout:
cols: 2
padding : 30,30
spacing: 10, 10
row_default_height: '40dp'
CustomLabel:
text: 'State Name'
SingleLineTextInput:
id: stateName
on_text: root.display_groups(self)
CustomLabel:
text: 'State Code'
SingleLineTextInput:
id: stateCode
CustomLabel:
text: 'City Name'
SingleLineTextInput:
id: cityName
CustomLabel:
text: 'Short Name'
SingleLineTextInput:
id: shortName
CustomLabel:
text: 'Pin Code'
SingleLineTextInput:
id: pinCode
GreenButton:
text: 'Ok'
GreenButton:
text: 'Cancel'
Label:
Label:
任何人都可以帮助我吗? 1.如何在&#39; Test1&#39;上添加搜索过滤器textBox。我们可以从List中搜索。如果任何人键入Test1,那么只应该从treeview显示test1。 2. Test1的行为类似于所有node.how的父节点。删除此属性。如何将所有节点更改为类似父节点 3.现在test1,test2,test3正在进行static.how变为动态。
答案 0 :(得分:2)
- Test1的行为类似于所有node.how的父节点,以删除此属性。如何将所有节点更改为类似父节点
醇>
您可以将Test1
设置为子节点,然后将treeView root_options的text属性设置为''
...
tree = [{'node_id': 'Test1',
'children': []},
{'node_id': 'Test2',
'children': []},
{'node_id': 'Test3',
'children': []}]
...
class TreeviewGroup(Popup):
treeview = ObjectProperty(None)
tv = ObjectProperty(None)
def __init__(self, **kwargs):
super(TreeviewGroup, self).__init__(**kwargs)
self.tv = TreeView(root_options=dict(text=""),
hide_root=False,
indent_level=4)
...
- 如何在&#39; Test1&#39;上添加搜索过滤器文本框。我们可以从List中搜索。如果有人键入Test1,那么只应该从treeview显示test1。
醇>
您可以在弹出窗口上定义一个过滤器方法来处理它,例如:
class TreeviewGroup(Popup):
...
def filter(self, f):
self.treeview.clear_widgets()
self.tv = TreeView(root_options=dict(text=""),
hide_root=False,
indent_level=4)
new_tree = []
for n in tree:
if f.lower() in n['node_id'].lower():
new_tree.append(n)
for branch in new_tree:
populate_tree_view(self.tv, None, branch)
self.treeview.add_widget(self.tv)
然后在用户输入重要州名称时调用它:
...
class GroupScreen(Screen):
groupName = ObjectProperty(None)
popup = ObjectProperty(None)
def display_groups(self, instance):
if len(instance.text) > 4: #I choose 4 because it is the smallest length of your nodes
if self.popup is None:
self.popup = TreeviewGroup()
self.popup.filter(instance.text)
self.popup.open()
我希望这有帮助!
更新:您的上一次请求
将ti属性添加到弹出窗口(textinput):
... class TreeviewGroup(Popup): treeview = ObjectProperty(无) tv = ObjectProperty(无) ti = ObjectProperty() ...
然后编辑 .kv 文件:
...
<TreeviewGroup>:
id: treeview
treeview: treeview
title: "Select City"
ti: ti
size_hint: None, None
size: 400, 400
auto_dismiss: False
BoxLayout
orientation: "vertical"
TextInput:
id: ti
size_hint_y: .1
on_text: root.filter(self.text)
BoxLayout:
id: treeview
Button:
size_hint: 1, 0.1
text: "Close"
on_release: root.dismiss()
...
不要忘记编辑display_groups方法:
...
class GroupScreen(Screen):
groupName = ObjectProperty(None)
popup = ObjectProperty(None)
def display_groups(self, instance):
if len(instance.text) > 0:
if self.popup is None:
self.popup = TreeviewGroup()
self.popup.filter(instance.text)
self.popup.open()
...
# cur.execute("SELECT * FROM `m_state` order by state_id asc")
# rows = cur.fetchall()
# print(rows)
rows = [(1, 'test1', 111), (2, 'test2', 112), (3, 'test3', 113), (4, 'test4', 114)]
tree = []
for r in rows:
tree.append({'node_id': r[1], 'children': []})
...