python / kivy:在treeView的TextInput中为on_touch_down事件设置动态值

时间:2018-01-02 13:56:59

标签: python python-3.x python-2.7 kivy kivy-language

我有两个文件test1.pytest1.kv。有一个+Add more按钮。点击+Add More按钮然后行添加动态。
我在TextInput上添加了treeView。但现在我很困惑如何将所选的treeView值(self.text)放在当前的TextBox中,因为每个TextBox都添加了动态,所以我不知道如何在当前treeView

中设置选定的TextBox

test1.py

import kivy

kivy.require('1.9.0')  # replace with your current kivy version !
import sqlite3 as lite
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import BooleanProperty, ListProperty, StringProperty, ObjectProperty, NumericProperty
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.core.window import Window
from kivy.uix.label import Label
from kivy.clock import Clock
from kivy.uix.treeview import TreeView, TreeViewLabel, TreeViewNode
from kivy.uix.screenmanager import Screen

Window.size = (450, 525)


class TreeviewNumber(Popup):
    treeview = ObjectProperty(None)
    tv = ObjectProperty(None)
    h = NumericProperty(0)


    def __init__(self, **kwargs):
        super(TreeviewNumber, self).__init__(**kwargs)
        self.tv = TreeView(root_options=dict(text=""),
                       hide_root=False,
                       indent_level=4)

        rows = [('One'), ('Two'), ('Three')]

        tree = []

        for r in rows:
            tree.append({'node_id': r, 'children': []})

        for branch in tree:
            populate_tree_view(self.tv, None, branch)
        #self.remove_widgets()
        self.treeview.add_widget(self.tv)
        Clock.schedule_once(self.update, 1)

    def remove_widgets(self):
        for child in [child for child in self.treeview.children]:
            self.treeview.remove_widget(child)

    def update(self, *args):
        self.h = len([child for child in self.tv.children]) * 24

def populate_tree_view(tree_view_city, parent, node):

    if parent is None:
        tree_node = tree_view_city.add_node(TreeViewLabel(text=node['node_id'],
                                                     is_open=True))
    else:
        tree_node = tree_view_city.add_node(TreeViewLabel(text=node['node_id'],
                                                     is_open=True), parent)

    for child_node in node['children']:
        populate_tree_view(tree_view_city, tree_node, child_node)

class TreeViewLabel(Label, TreeViewNode):
    pass

class display(Screen):

    def add_more(self):
        self.ids.rows.add_row()

    def insert_value(self):
        #print(Row().id)
        w = Row()
        print(w.ids.state.text)



class Row(BoxLayout):
    button_text = StringProperty("")
    state = ObjectProperty('')

    popupNumber = ObjectProperty(None)
    popup_number = ObjectProperty(None)

    def display_number(self,instance):
        if self.popupNumber is None:
            # if self.popupAccountCity is None:
            self.popupNumber = TreeviewNumber()
            self.popupNumber.popup_number = self
        self.popupNumber.open()


class Rows(BoxLayout):
    orientation = "vertical"
    row_count = 0
    button_text = StringProperty("")

    def __init__(self, **kwargs):
        super(Rows, self).__init__(**kwargs)
        self.add_row()

    def add_row(self):
        self.row_count += 1
        self.add_widget(Row(button_text=str(self.row_count),id=str("test"+str(self.row_count))))


class test(App):

    def build(self):
        self.root = Builder.load_file('test1.kv')
        return self.root

if __name__ == '__main__':
    test().run()

test1.kv

<Row>:
    state : state
    orientation: "horizontal"
    spacing: 0, 5

    Button:
        text: root.button_text
        size_hint_x: .2

    TextInput:
        #text : root.id
        size_hint_x: .8
        id : state
        on_focus: root.display_number(self)



display:

    BoxLayout:
        orientation: "vertical"
        padding : 20, 20

        BoxLayout:
            orientation: "horizontal"

            Button:
                size_hint_x: .2
                text: "+Add More"
                valign: 'bottom'
                on_press: root.add_more()


        BoxLayout:
            orientation: "horizontal"

            Label:
                size_hint_x: .2
                text: "SN"
                valign: 'bottom'

            Label:
                size_hint_x: .8
                text: "Value"
                valign: 'bottom'


        Rows:
            id: rows

        BoxLayout:
            orientation: "horizontal"
            padding : 10, 0
            spacing: 10, 10
            size_hint: .5, .7
            pos_hint: {'x': .25, 'y':.25}

            Button:
                text: 'Ok'
                on_release:
                    root.insert_value()

            Button:
                text: 'Cancel'
                on_release: root.dismiss()

<TreeviewNumber>:
    treeview: treeview
    title: "Select Number"
    title_size: 17
    title_font: "Verdana"
    #pos_hint: {'x': .62, 'y': .55}
    size_hint: .5, .5
    auto_dismiss: False

    BoxLayout
        orientation: "vertical"

        ScrollView:
            size_hint: 1, .9
            BoxLayout:
                size_hint_y: None
                id: treeview
                #height: root.h
                #rooot: root

        GridLayout:
            cols : 2
            row_default_height: '20dp'
            size_hint: .5, 0.2
            pos_hint: {'x': .25, 'y': 1}

            Button:
                text: 'Ok'
                on_release: root.dismiss()

            Button:
                text: 'Cancel'
                on_release: root.dismiss()

<TreeViewLabel>:
    text_size: self.size
    height: 24
    on_touch_down:
        print(self.text)

0 个答案:

没有答案