import kivy
kivy.require('1.9.0') # replace with your current kivy version !
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
Window.size = (500, 330)
con = lite.connect('demo.db')
con.text_factory = str
cur = con.cursor()
class GroupScreen(Screen):
pass
class Group(App):
#cur.execute("SELECT * FROM `groups` order by group_id asc")
#rows = cur.fetchall()
rows = [(1, 'Branch/Dividions', ''), (2, 'Capital Account', 'Reserve & Surplus')]
def build(self):
self.root = Builder.load_file('a.kv')
return self.root
if __name__ == '__main__':
Group().run()
GroupScreen:
GridLayout:
cols: 2
padding : 30,30
spacing: 10, 10
row_default_height: '40dp'
Label:
text: 'Group'
SingleLineTextInput:
id: groupName
Label:
text: 'Leadger No.'
SingleLineTextInput:
id: groupCode
GreenButton:
text: 'Ok'
GreenButton:
text: 'Cancel'
Label:
Label:
<SingleLineTextInput@TextInput>:
multiline: False
<GreenButton@Button>:
background_color: 1, 1, 1, 1
size_hint_y: None
height: self.parent.height * 0.150
[![在此处输入图像说明] [1]] [1]
我有a.py和a.kv文件 1.当我在Test textBox中键入任何关键字时,应该打开一个带有treeview结构的新窗口 2.当从树视图中选择任何值时,它将被放置在文本框中,应该禁用手动输入。
有人可以帮助我吗?
答案 0 :(得分:2)
以下示例使用 on_text 事件来调用包含 treeview 和取消按钮的弹出小部件。每当文本发生变化时,都会调度on_text事件。
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.size = (500, 330)
# con = lite.connect('demo.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)
tree = [{'node_id': 'Capital Account',
'children': [{'node_id': 'Reserves & Surplus',
'children': [{'node_id': '1.1.1',
'children': [{'node_id': '1.1.1.1',
'children': []}]},
{'node_id': '1.1.2',
'children': []},
{'node_id': '1.1.3',
'children': []}]},
{'node_id': '1.2',
'children': []}]},
{'node_id': 'Current Assests',
'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="Branch/Divisions"),
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):
#cur.execute("SELECT * FROM `groups` order by group_id asc")
#rows = cur.fetchall()
rows = [(1, 'Branch/Dividions', ''), (2, 'Capital Account', 'Reserve & Surplus')]
def build(self):
self.root = Builder.load_file('test.kv')
return self.root
if __name__ == '__main__':
Group().run()
#:kivy 1.10.0
<TreeViewLabel>:
on_touch_down:
app.root.groupName.text = self.text
app.root.popup.dismiss()
<TreeviewGroup>:
id: treeview
treeview: treeview
title: "Groups"
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:
groupName: groupName
GridLayout:
cols: 2
padding : 30,30
spacing: 10, 10
row_default_height: '40dp'
CustomLabel:
text: 'Group'
SingleLineTextInput:
id: groupName
on_text: root.display_groups(self)
CustomLabel:
text: 'Leadger No.'
SingleLineTextInput:
id: groupCode
GreenButton:
text: 'Ok'
GreenButton:
text: 'Cancel'
Label:
Label: