简体   繁体   中英

python/kivy : access attribute value in .kv file

When I click on Account(root.display_account()) then call display_account().After that RVACCOUNT() function call .After that when i click on +Add Account then def add_account(self): call

I have a class AccountPopup which define a attribute state_text and assign value text:'Testing' in .kv file
How to get value of state_text 'Testing' and pass in on_text: root.filter(self.text,state_text) and print in def filter function.

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()

Can someone help me?

You should reference it as self.state_text everywhere, also make it a StringProperty in the py file and can than access it as

on_text: root.filter(self.text,root.state_text)

root in kv refers to the most left widget aka <TreeviewCityAccount>: in your case.

See https://kivy.org/docs/api-kivy.lang.html 在此处输入图片说明

Alternatively you can work with ids in the kv file.

the value you are looking for is not in your immediate root which is why this is not working.The say thing to do is get the full path to that property like so: Snippet :

<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

Also,generally,it's a good idea to id your classes mate

Disclaimer:code not tested

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM