python / kivy:访问.kv文件中的属性值

时间:2017-12-02 17:04:12

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

当我点击帐户(root.display_account())然后调用display_account()。之后调用RVACCOUNT()函数。之后当我点击+添加帐户然后def add_account(self):call

我有一个类AccountPopup,它定义了一个属性state_text并赋值文本:'testing'在.kv文件中
如何获取state_text'Testing'的值并传入on_text:root.filter(self.text,state_text)并在def过滤器函数中打印。

test.py

class AccountPopup(Popup):
    state_text = ObjectProperty(None)
    popupAccountCity = ObjectProperty(None)


    def display_cities_treeview_account(self, instance):
        if len(instance.text) > 0:
            #if self.popupAccountCity is None:
            self.popupAccountCity = TreeviewCityAccount(self.state_text.text)
            self.popupAccountCity.filter(instance.text,self.state_text.text)
        self.popupAccountCity.open()


class TreeviewCityAccount(Popup):
    state_text = ObjectProperty(None)

    def __init__(self,state_text, **kwargs):
        print(state_text)

    def filter(self, f,state):
        print(state)


class RVACCOUNT(BoxLayout):
    def add_account(self):
        self.mode = "Add"
        popup = AccountPopup(self)
        popup.open()

class MainMenu(BoxLayout):

    def display_account(self):
        self.dropdown.dismiss()
        self.remove_widgets()
        self.rvaccount = RVACCOUNT()
        self.content_area.add_widget(self.rvaccount)


class FactApp(App):
    title = "Test"

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



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

test.kv

<AccountPopup>:
    state_text:state_text

        TextInput:
            id:state_text
            text:'Testing'

<TreeviewCityAccount>:

    BoxLayout
        orientation: "vertical"

        TextInput:
            id: treeview
            size_hint_y: .1
            on_text: root.filter(self.text,state_text)

<RVACCOUNT>:
    BoxLayout:
        orientation: "vertical"
        Button:
            size_hint: .07, .03
            text: "+Add Account"
            on_press: root.add_account()


<MainMenu>:
    content_area: content_area
    dropdown: dropdown

    BoxLayout:
        orientation: 'vertical'
        #spacing : 10

        BoxLayout:
            canvas.before:
                Rectangle:
                    pos: self.pos
                    size: self.size

            MenuButton:
                    id: btn
                    text: 'Master'
                    size : (60,30)
                    on_release: dropdown.open(self)

            CustDrop:

                DropdownButton:
                    text: 'Account'
                    size_hint_y: None
                    height: '32dp'
                    on_release: root.display_account()

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

你应该把它作为self.state_text引用到任何地方,也可以在py文件中将其作为StringProperty,然后可以将其作为

进行访问
on_text: root.filter(self.text,root.state_text)

root in kv指的是你的案例中最左边的小部件<TreeviewCityAccount>:

参见https://kivy.org/docs/api-kivy.lang.html enter image description here

或者,您可以使用kv文件中的ID。

答案 1 :(得分:0)

您要查找的值不在您的直接根目录中,这就是为什么它不起作用的原因。要做的事情是获取该属性的完整路径,如下所示: 摘要

<AccountPopup>:
    id: ac_popup
    #bunch of code
 <TreeviewCityAccount>:
    #chunk of code
    TextInput:
       id: tree view
       on_text:root.filter(self.text,app.ac_popup.state_text

通常,最好让您的班级同学相识

免责声明:代码未经测试