2017-12-02 130 views
0

當我點擊Account(root.display_account())然後調用display_account()。之後,RVACCOUNT()函數調用。之後,當我點擊+添加帳戶然後高清add_account(個體經營):調用
python/kivy:在.kv文件中訪問屬性值

我有一個類AccountPopup其定義屬性state_text和賦值文字:在.kv文件「測試」
如何獲得state_text「測試」的價值,並通過在on_text: root.filter(self.text,state_text)並在def filter中打印。

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

有人能幫助我嗎?

+0

你在哪裏打開AccountPopup? – EL3PHANTEN

回答

0

你應該引用它作爲self.state_text無處不在,也使其在PY文件,可以一比StringProperty訪問它作爲

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

根KV是指在你的情況下,最左邊的小部件又名<TreeviewCityAccount>:

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

或者你可以在KV文件ID工作。