Reputation: 406
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 = (700, 530)
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': '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):
#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)]
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'
Spinner:
text: "State Code"
values: ["111", "112", "113", "114"]
#background_color: color_button if self.state == 'normal' else color_button_pressed
background_down: 'atlas://data/images/defaulttheme/spinner'
#color: color_font
#option_cls: Factory.get("MySpinnerOption")
#size_hint: None, None
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:
Can someone help me?
1. In above image state code shows 111,112,113,114 which are static.How to show dynamic these state code.I am retrieve data from database which looks
rows = [(1, 'test1', 111), (2, 'test2', 112), (3, 'test3', 113), (4, 'test4', 114)]
In third index value are coming 111,112,113,114.How to put these value in spinner.
Upvotes: 1
Views: 1105
Reputation: 2645
you can set a listproperty attribute in your group class then in the kv set the values of the spinner with this list:
in the .py:
...
class Group(App):
#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)]
r = ListProperty()
r = [str(t[2]) for t in rows]
...
then in the .kv:
...
Spinner:
text: "State Code"
values: app.r
...
I have used Lisproperty in case the values change in the future
Upvotes: 3