简体   繁体   English

Python / Kivy:将变量从一类传递到另一类

[英]Python/Kivy : pass a variable from one class to another class

I have a class GroupScreen which has a variable self.abc = 10 then how to get this variable value in TreeviewGroup class and pass in this query cur.execute("SELECT * FROM test WHERE id =?", (abc,)) 我有一个GroupScreen类, GroupScreen具有一个变量self.abc = 10然后如何在TreeviewGroup类中获取此变量值并传递此查询cur.execute("SELECT * FROM test WHERE id =?", (abc,))

class GroupScreen(Screen):
    groupName = ObjectProperty(None)
    popup = ObjectProperty(None)
    abc = ObjectProperty(None)

    def display_groups(self, instance):
        self.abc    =   10
        print(self.abc)
        if len(instance.text) > 0:
            self.popup = TreeviewGroup(self.abc)
            self.popup.open()

class TreeviewGroup(Popup):
    treeview = ObjectProperty(None)
    tv = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(TreeviewGroup, self).__init__(**kwargs)
        self.tv = TreeView(root_options=dict(text=""),
                           hide_root=False,
                           indent_level=4)


        cur.execute("SELECT * FROM `test` WHERE `id`=?", (abc,))
        rows = cur.fetchall()
        print(rows)
        # rows = [(1, 'test1'), (2, 'test2'), (3, 'test3')]
        tree = []

        for r in rows:
            tree.append({'node_id': r[1], 'children': []})
        for branch in tree:
            populate_tree_view(self.tv, None, branch)
        self.remove_widgets()
        self.treeview.add_widget(self.tv)

    def remove_widgets(self):
        for child in [child for child in self.treeview.children]:
            self.treeview.remove_widget(child)

Adding abc to your __init__ should work: abc添加到__init__应该可以:

class TreeviewGroup(Popup):
    treeview = ObjectProperty(None)
    tv = ObjectProperty(None)

    def __init__(self, abc, **kwargs): # note the `abc`
        ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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