如何在 GUI-init 上初始化 Kivy TreeView?
How to initialize a Kivy TreeView on GUI-init?
我正在尝试实现一个动态 TreeView,其中所有更新都在 kivy 中初始化并调用一个名为 populate_tree_view(self, tree) 的函数。关于这种方法,可用的 Tree-View-docs 对我来说有点神秘......我已经无法在应用程序的 init 上填充 TreeView。对于以下代码,我收到错误:
name "wid" is not defined
这怎么可能?据我了解,我指的是 self=Widget,这个小部件有一个名为“wid”的 TreeView。请帮助我。
我的 kivi 文件:
<Widget>
TreeView:
id: wid
root_options: dict(text=somename)
我的python代码:
class Widget(StackLayout):
def populate_tree_view(self, tree):
self.wid.add_node(TreeViewLabel(text='My first item'))
print("done")
# Init GUI
class App (App):
def build(self):
App = Widget()
App.populate_tree_view(tree)
return App
App().run()
几个问题:
- 您需要使用
Clock.schedule_once()
来调用您的 populate_tree_view()
方法。这将延迟调用,直到 wid
在 ids
中可用。将 Clock.schedule_once()
放在 build()
方法中 return
. 之前
- 要访问
wid
,您必须使用 ids
词典 (self.ids.wid.add_node(TreeViewLabel(text='My first item')
)
App
是 Kivy 包中的 class。将其重新定义为 Widget
实例(或者甚至是 App
class 的名称)不是一个好主意。只是不要将 App =
设置为任何东西,也不要使用 class App():
.
我正在尝试实现一个动态 TreeView,其中所有更新都在 kivy 中初始化并调用一个名为 populate_tree_view(self, tree) 的函数。关于这种方法,可用的 Tree-View-docs 对我来说有点神秘......我已经无法在应用程序的 init 上填充 TreeView。对于以下代码,我收到错误:
name "wid" is not defined
这怎么可能?据我了解,我指的是 self=Widget,这个小部件有一个名为“wid”的 TreeView。请帮助我。
我的 kivi 文件:
<Widget>
TreeView:
id: wid
root_options: dict(text=somename)
我的python代码:
class Widget(StackLayout):
def populate_tree_view(self, tree):
self.wid.add_node(TreeViewLabel(text='My first item'))
print("done")
# Init GUI
class App (App):
def build(self):
App = Widget()
App.populate_tree_view(tree)
return App
App().run()
几个问题:
- 您需要使用
Clock.schedule_once()
来调用您的populate_tree_view()
方法。这将延迟调用,直到wid
在ids
中可用。将Clock.schedule_once()
放在build()
方法中return
. 之前
- 要访问
wid
,您必须使用ids
词典 (self.ids.wid.add_node(TreeViewLabel(text='My first item')
) App
是 Kivy 包中的 class。将其重新定义为Widget
实例(或者甚至是App
class 的名称)不是一个好主意。只是不要将App =
设置为任何东西,也不要使用class App():
.