bla*_*kel 5 python python-3.x kivy kivy-language
在我的 .py 文件中,我有一个看起来像这样的屏幕:
class ExampleScreen(Screen):
def create_layout(self):
box = BoxLayout(orientation='vertical')
# other layout stuff in here
self.add_widget(box)
Run Code Online (Sandbox Code Playgroud)
在我的 .kv 文件中,我有一个按钮,按下该按钮会调用此函数并在 .kv 文件上显示此布局<ExampleScreen>。但是,我希望能够按下此按钮并首先检查此布局是否已存在,如果存在,请在添加新布局之前将其删除。我预计会修改create_layout()为:
def create_layout(self):
if (box layout child has already been added):
self.remove_widget(box layout child)
box = BoxLayout(orientation='vertical')
# other layout stuff in here
self.add_widget(box)
Run Code Online (Sandbox Code Playgroud)
有谁知道如何做到这一点?以某种方式使用id?
每个小部件都有一个Children属性,因此您可能需要使用它。
for c in list(self.children):
if isinstance(c, BoxLayout): self.remove(c)
Run Code Online (Sandbox Code Playgroud)
您还可以将其分配给小部件:(如 Edvardas Dlugauskas anwser 中所述)
def __init__(self, **kw):
self.box = None
...
def create_layout(self):
if self.box: self.remove(self.box)
self.box = BoxLayout(orientation='vertical')
self.add_widget(box)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4772 次 |
| 最近记录: |