daily pastebin goal
72%
SHARE
TWEET

Untitled

a guest Nov 21st, 2017 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import sqlite3 as lite
  2.  
  3. from kivy.uix.screenmanager import Screen
  4. from kivy.app import App
  5. from kivy.lang import Builder
  6. from kivy.core.window import Window
  7. from kivy.uix.popup import Popup
  8. from kivy.uix.treeview import TreeView, TreeViewLabel, TreeViewNode
  9. from kivy.uix.label import Label
  10. from kivy.properties import ObjectProperty,StringProperty
  11. import sys
  12. Window.clearcolor = (.152, .149, .149, 0)
  13. Window.size = (700, 330)
  14.  
  15.  
  16. def populate_tree_view(tree_view, parent, node):
  17.     if parent is None:
  18.         tree_node = tree_view.add_node(TreeViewLabel(text=node['node_id'],
  19.                                                      is_open=True))
  20.     else:
  21.         tree_node = tree_view.add_node(TreeViewLabel(text=node['node_id'],
  22.                                                      is_open=True), parent)
  23.  
  24.     for child_node in node['children']:
  25.         populate_tree_view(tree_view, tree_node, child_node)
  26.  
  27. rows = [('1','1','city1'),('2','2','city2'),('3','3','city3')]
  28. tree = []
  29.  
  30. for r in rows:
  31.     tree.append({'node_id': r[2], 'children': []})
  32.  
  33.  
  34. class TreeViewLabel(Label, TreeViewNode):
  35.     pass
  36.  
  37. class TreeviewGroup(Popup):
  38.     treeview = ObjectProperty(None)
  39.     tv = ObjectProperty(None)
  40.     #ti = ObjectProperty()
  41.  
  42.     def __init__(self, **kwargs):
  43.         super(TreeviewGroup, self).__init__(**kwargs)
  44.         self.tv = TreeView(root_options=dict(text=""),
  45.                        hide_root=False,
  46.                        indent_level=4)
  47.         for branch in tree:
  48.             populate_tree_view(self.tv, None, branch)
  49.         self.remove_widgets()
  50.         self.treeview.add_widget(self.tv)
  51.  
  52.     def remove_widgets(self):
  53.         for child in [child for child in self.treeview.children]:
  54.             self.treeview.remove_widget(child)
  55.  
  56.     def filter(self, f):
  57.         self.treeview.clear_widgets()
  58.         self.tv = TreeView(root_options=dict(text=""),
  59.                            hide_root=False,
  60.                            indent_level=4)
  61.         new_tree = []
  62.         for n in tree:
  63.             if f.lower() in n['node_id'].lower():
  64.                 new_tree.append(n)
  65.         for branch in new_tree:
  66.             populate_tree_view(self.tv, None, branch)
  67.  
  68.         self.treeview.add_widget(self.tv)
  69.  
  70. def populate_tree_view_area(tree_view, parent, node):
  71.     if parent is None:
  72.         tree_node = tree_view.add_node(TreeViewLabel(text=node['node_id'],
  73.                                                      is_open=True))
  74.     else:
  75.         tree_node = tree_view.add_node(TreeViewLabel(text=node['node_id'],
  76.                                                      is_open=True), parent)
  77.  
  78.     for child_node in node['children']:
  79.         populate_tree_view(tree_view, tree_node, child_node)
  80.  
  81. rows = [('1','1','area1'),('2','2','area2'),('3','3','area3')]
  82. treeArea = []
  83.  
  84. for r in rows:
  85.     treeArea.append({'node_id': r[2], 'children': []})
  86.  
  87. class TreeviewArea(Popup):
  88.     treeviewArea = ObjectProperty(None)
  89.     tv = ObjectProperty(None)
  90.     #ti = ObjectProperty()
  91.  
  92.     def __init__(self, **kwargs):
  93.         super(TreeviewArea, self).__init__(**kwargs)
  94.         self.tv = TreeView(root_options=dict(text=""),
  95.                        hide_root=False,
  96.                        indent_level=4)
  97.         for branch in treeArea:
  98.             populate_tree_view_area(self.tv, None, branch)
  99.         self.remove_widgets()
  100.         self.treeviewArea.add_widget(self.tv)
  101.  
  102.     def remove_widgets(self):
  103.         for child in [child for child in self.treeviewArea.children]:
  104.             self.treeviewArea.remove_widget(child)
  105.  
  106. class AreaScreen(Screen):
  107.     groupName = ObjectProperty(None)
  108.     popup = ObjectProperty(None)
  109.     #statecode = StringProperty('')
  110.  
  111.     def display_cities(self, instance):
  112.         if len(instance.text) > 0:
  113.             if self.popup is None:
  114.                 self.popup = TreeviewGroup()
  115.             #self.popup.filter(instance.text)
  116.             self.popup.open()
  117.  
  118.     def display_areas(self, instance):
  119.         if len(instance.text) > 0:
  120.             if self.popup is None:
  121.                 self.popup = TreeviewGroup()
  122.             #self.popup.filter(instance.text)
  123.             self.popup.open()
  124.  
  125.  
  126. class FactArea(App):
  127.     def build(self):
  128.         self.root = Builder.load_file('test.kv')
  129.         return self.root
  130.  
  131.  
  132.  
  133. if __name__ == '__main__':
  134.     FactArea().run()
  135.    
  136. <TreeViewLabel>:
  137.     on_touch_down:
  138.         app.root.cityName.text = self.text
  139.         #app.root.select_node(self)
  140.         app.root.popup.dismiss()
  141.  
  142. <TreeviewGroup>:
  143.     id: treeview
  144.     treeview: treeview
  145.     title: "Select City"
  146.     size_hint: None, None
  147.     size: 400, 350
  148.     auto_dismiss: False
  149.  
  150.     BoxLayout
  151.         orientation: "vertical"
  152.         #TextInput:
  153.             #id: ti
  154.             #size_hint_y: .1
  155.             #on_text: root.filter(self.text)
  156.         BoxLayout:
  157.             id: treeview
  158.             #on_press: root.select_node(self.text)
  159.         Button:
  160.             size_hint: 1, 0.1
  161.             text: "Close"
  162.             on_release: root.dismiss()
  163.  
  164. <TreeviewGroupArea>:
  165.     id: treeviewArea
  166.     treeviewArea: treeviewArea
  167.     title: "Select City"
  168.     size_hint: None, None
  169.     size: 400, 350
  170.     auto_dismiss: False
  171.  
  172.     BoxLayout
  173.         orientation: "vertical"
  174.         #TextInput:
  175.             #id: ti
  176.             #size_hint_y: .1
  177.             #on_text: root.filter(self.text)
  178.         BoxLayout:
  179.             id: treeviewArea
  180.             #on_press: root.select_node(self.text)
  181.         Button:
  182.             size_hint: 1, 0.1
  183.             text: "Close"
  184.             on_release: root.dismiss()
  185.  
  186. <CustomLabel@Label>:
  187.     text_size: self.size
  188.     valign: "middle"
  189.     padding_x: 5
  190.  
  191. <SingleLineTextInput@TextInput>:
  192.     multiline: False
  193.  
  194.  
  195. <GreenButton@Button>:
  196.     background_color: 1, 1, 1, 1
  197.     size_hint_y: None
  198.     height: self.parent.height * 0.200
  199.  
  200. AreaScreen:
  201.     cityName: cityName
  202.  
  203.     GridLayout:
  204.         cols: 2
  205.         padding : 30,30
  206.         spacing: 10, 10
  207.         row_default_height: '30dp'
  208.  
  209.         Label:
  210.             text: 'City'
  211.             text_size: self.size
  212.             valign: 'middle'
  213.             padding_x: 50
  214.  
  215.  
  216.         SingleLineTextInput:
  217.             id: cityName
  218.             font_size_valign:self.height*0.4
  219.             on_text: root.display_cities(self)
  220.  
  221.         Label:
  222.             text: 'Area'
  223.             text_size: self.size
  224.             valign: 'middle'
  225.             padding_x: 50
  226.  
  227.  
  228.         SingleLineTextInput:
  229.             id: areaName
  230.             on_text: root.display_areas(self)
  231.  
  232.  
  233.         GreenButton:
  234.             text: 'Ok'
  235.             on_press: root.insert_data(cityName.text, areaName.text)
  236.  
  237.  
  238.  
  239.         GreenButton:
  240.             text: 'Cancel'
  241.             on_press: app.stop()
  242.    
  243. ...
  244. def populate_tree_view_area(tree_view, parent, node):
  245.     if parent is None:
  246.         tree_node = tree_view.add_node(TreeViewLabelArea(text=node['node_id'],
  247.                                                  is_open=True))
  248.     else:
  249.         tree_node = tree_view.add_node(TreeViewLabelArea(text=node['node_id'],
  250.                                                  is_open=True), parent)
  251.  
  252.     for child_node in node['children']:
  253.         populate_tree_view(tree_view, tree_node, child_node)
  254.  
  255. rowsa = [('1','1','area1'),('2','2','area2'),('3','3','area3')]
  256. treeArea = []
  257.  
  258. for r in rowsa:
  259.     treeArea.append({'node_id': r[2], 'children': []})
  260.  
  261. class TreeViewLabelArea(Label, TreeViewNode):
  262.     pass
  263.  
  264. class TreeviewArea(Popup):
  265. #You can let it as it is
  266.  
  267. ...
  268.  
  269. class AreaScreen(Screen):
  270.     groupName = ObjectProperty(None)
  271.     popupcity = ObjectProperty(None)
  272.     popuparea = ObjectProperty(None)
  273.     #statecode = StringProperty('')
  274.  
  275.     ...
  276.  
  277.     def display_areas(self, instance):
  278.         if len(instance.text) > 0:
  279.             if self.popuparea is None:
  280.                 self.popuparea = TreeviewArea()
  281.             #self.popup.filter(instance.text)
  282.             self.popuparea.open()
  283.    
  284. ...
  285. <TreeViewLabelArea>:
  286.     height: 24
  287.     on_touch_down:
  288.         app.root.areaName.text = self.text
  289.         #app.root.select_node(self)
  290.         app.root.popuparea.dismiss()
  291.    
  292. ...
  293. AreaScreen:
  294.     cityName: cityName
  295.     areaName: areaName
  296. ...
RAW Paste Data
Top