Python 如何引用在KV文件中创建的实例

Python 如何引用在KV文件中创建的实例,python,kivy,instance,Python,Kivy,Instance,当在AddCategoryPopup类中按SAVE按钮时,我想从TreeCategory类调用一个方法add\u category\u to_tree()。但是,我在如何引用在KV文件中创建的TreeCategory实例时遇到了问题。 我试图寻找解决方案,但没有任何效果。当前我得到了AttributeError:“super”对象没有属性“\uu getattr\uuuu”错误 我应该如何正确地做到这一点?谢谢你的帮助 class TreeCategory(TreeView): def

当在
AddCategoryPopup
类中按SAVE按钮时,我想从
TreeCategory
类调用一个方法
add\u category\u to_tree()
。但是,我在如何引用在KV文件中创建的TreeCategory实例时遇到了问题。 我试图寻找解决方案,但没有任何效果。当前我得到了
AttributeError:“super”对象没有属性“\uu getattr\uuuu”
错误

我应该如何正确地做到这一点?谢谢你的帮助

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文件:

<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
:
盒子布局:
方向:“垂直”
文本输入:
id:条目
多行:False
提示文字:“Podaj nazwękategorii…”
盒子布局:
方向:“水平”
按钮:
文本:“保存”
按:root.save()
按钮:
文本:“取消”
按:root.close()
:
方向:“垂直”
显示:输入
树:树
盒子布局:
id:菜单
大小\u提示\u y:.1
按钮:
文字:“Dodaj kategorię”
按:root.add_category_按钮()
盒子布局:
id:recipe\u视图
方向:“水平”
树状分类:
id:树
隐藏根:对
大小提示:.25,1

你可以用很多方法来做。例如,您可以将.kv放在主.py文件中

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

您可以简单地命名.kv文件

guicookbookapp.kv 
并将其保留在项目的根目录中

您还可以添加以下内容

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

希望我正确理解了您的问题。

使用Python中的
self.ids
,您只能从该特定类访问KV中的ID。因此,
self.ids.tree
只能在Python中的
main screen
类中使用,而不能在
AddCategoryPopup
类中使用

您可以在
AddCategoryPopup
规则中创建
ObjectProperty
“topwidget”,并在实例化弹出窗口时传入主类。比如:

popup = AddCategoryPopup(topwidget=self)
然后在自定义弹出类的“保存”方法中,您可以执行以下操作:

self.topwidget.tree...

谢谢你的解释:)现在我明白它应该如何工作了