我编写了以下代码:
#-*- coding: utf-8 -*-
from kivy.lang import Builder
Builder.load_string("""
<TestWidget>:
BoxLayout:
id: rootBoxLayout
orientation: 'vertical'
size: root.size
BoxLayout:
Button:
text: "Button1"
BoxLayout:
Button:
text: "Button2"
BoxLayout:
Button:
text: "Button3"
BoxLayout:
Button:
text: "Button4"
BoxLayout:
Button:
text: "Button5"
Button:
text: "removeAllBoxLayout"
font_size: 48
on_press: root.removeAllBoxLayout()
""")
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
class TestWidget(Widget):
def __init__(self, **kwargs):
super(TestWidget, self).__init__(**kwargs)
def removeAllBoxLayout(self):
for row1 in self.ids.rootBoxLayout.children:
if isinstance(row1, BoxLayout):
self.ids.rootBoxLayout.remove_widget(row1)
class TestApp(App):
def __init__(self, **kwargs):
super(TestApp, self).__init__(**kwargs)
def build(self):
return TestWidget()
if __name__ == '__main__':
TestApp().run()
rootBoxLayout中有五个boxlayout
当我按下“removeAllBoxLayout”按钮时,我想删除所有5个BoxLayout
但当我按下“removeAllBoxLayout”按钮时,它实际上只删除了3个按钮
如何删除作为rootBoxLayout子级的所有boxlayout
嗯,这确实是一个常见的疏忽。
Removing items of an iterable while iterating over it
修复它的方法是如下更改
removeAllBoxLayout
方法:相关问题 更多 >
编程相关推荐