2017-05-10 9 views
0

AddCategoryPopup 클래스에서 SAVE 버튼을 누를 때 TreeCategory 클래스에서 add_category_to_tree() 클래스를 호출하려고합니다. 그러나, 나는 KV 파일에서 생성 된 TreeCategory 인스턴스를 참조하는 방법에 문제가있다. 솔루션을 검색하려고했지만 아무 것도 작동하지 않았습니다. 현재 AttributeError: 'super' object has no attribute '__getattr__' 오류가 발생합니다.KV 파일로 생성 된 인스턴스를 참조하는 방법

어떻게해야합니까? 당신은 그것을 꽤 몇 가지 방법을 수행 할 수 있습니다

<AddCategoryPopup>: 
    BoxLayout: 
     orientation: 'vertical' 
     TextInput: 
      id: entry 
      multiline: False 
      hint_text: 'Podaj nazwę kategorii...' 
     BoxLayout: 
      orientation: 'horizontal' 
      Button: 
       text: 'SAVE' 
       on_press: root.save() 
      Button: 
       text: 'CANCEL' 
       on_press: root.close() 

<MainScreen>: 
    orientation: "vertical" 
    display: entry 
    tree: tree 

    BoxLayout: 
     id: menu 
     size_hint_y: .1 

     Button: 
      text: 'Dodaj kategorię' 
      on_press: root.add_category_button() 

    BoxLayout: 
     id: recipe_view 
     orientation: "horizontal" 

     TreeCategory: 
      id: tree 
      hide_root: True 
      size_hint: .25, 1 

답변

0

파이썬에서 'self.ids'를 사용하면 해당 특정 클래스의 KV ID에만 액세스 할 수 있습니다. 따라서 'self.ids.tree'는 Python의 MainScreen 클래스 내에서만 AddCategoryPopup 클래스가 아닌 경우에만 사용할 수 있습니다. AddCategoryPopup 규칙에 ObjectProperty 'topwidget'을 만들고 팝업을 인스턴스화 할 때 Main 클래스를 전달할 수 있습니다. 뭔가 같은 : 팝업 = AddCategoryPopup (topwidget = 자기). 그런 다음 사용자 지정 팝업 클래스의 '저장'메서드에서 다음과 같은 작업을 수행 할 수 있습니다. self.topwidget.tree ...

+0

이 설명에 감사드립니다. 이제 어떻게 작동하는지 이해합니다. – r3nia

+0

환영합니다, 수락을 클릭하십시오 대답. – TomKivy

0

: 도움을

class TreeCategory(TreeView): 
    def __init__(self, **kwargs): 
     super(TreeCategory, self).__init__(**kwargs) 

    def add_category_to_tree(self, name): 
     self.add_node(TreeViewLabel(text = name.upper())) 


class AddCategoryPopup(Popup): 

    def save(self): 
     self.ids.tree.add_category_to_tree(self.ids.entry.text) # ???? 
     db.adding_to_db('kategorie', 'nazwa', self.ids.entry.text) 
     self.dismiss() 

    def close(self): 
     self.dismiss() 

class MainScreen(BoxLayout): 
    tree = ObjectProperty(None) 

    def add_category_button(self): 
     popup = AddCategoryPopup(title = 'Dodawanie nowej kategorii') 
     return popup.open() 


class GuiCookBookApp(App): 
    def build(self): 
     self.title = "Książka kucharska" 
     return MainScreen() 


if __name__ == "__main__": 
    db = DatabaseManager("cookbook.sqlite") 
    GuiCookBookApp().run() 

KV 파일을 주셔서 감사합니다. 예를 들어 .kv 파일을 주 .py 파일에 넣을 수 있습니다.

w = Builder.load_string(''' 
Widget: 
    height: self.width/2. if self.disabled else self.width 
    x: self.y + 50 
''') 

https://kivy.org/docs/api-kivy.lang.builder.html

당신은 단순히 .kv 파일

guicookbookapp.kv 

의 이름을 지정하고 프로젝트의 루트 디렉토리에 둘 수 있습니다.

https://kivy.org/docs/examples/gen__application__app_with_kv__py.html

또한 다음

from kivy.lang import Builder 
Builder.load_file('guicookbookapp.kv') 

가 잘하면 내가 제대로 질문을 이해 추가 할 수 있습니다.