使用Python动态删除Kivy中的整个boxLayout及其子项
如何使用子按钮删除父级使用Python动态删除Kivy中的整个boxLayout及其子项,python,kivy,Python,Kivy,如何使用子按钮删除父级BoxLayout? 请记住,我将有许多类似的盒子布局 这里是我创建每个BoxLayout并将其添加到全局布局itemsLayout def submitProductToWatch(self, productName, ebay, letGo, mercari): self.current = 'main' newItem = BoxLayout(id="boxID",size_hint_y=None, height=40)
BoxLayout
?
请记住,我将有许多类似的盒子布局
这里是我创建每个BoxLayout
并将其添加到全局布局itemsLayout
def submitProductToWatch(self, productName, ebay, letGo, mercari):
self.current = 'main'
newItem = BoxLayout(id="boxID",size_hint_y=None, height=40)
myCheck = CheckBox(id="checkID",size_hint_x=None, color=(0.467,.878,.259,1))
prodLabel= Label(text=productName, size_hint_x=None)
webCoverageString = self.displayWebsitesForProduct(ebay, letGo, mercari)
covLabel= Label(id="coverageID",text=webCoverageString)
remButton= Button(id="removeID",text="Remove", color=(1,0,0,.7),size_hint= (.6,.7),
font_size= 15)
newItem.add_widget(myCheck)
newItem.add_widget(prodLabel)
newItem.add_widget(covLabel)
newItem.add_widget(remButton)
itemsLayout.add_widget(newItem)
以下是一种方法:
class Main(Widget):
def __init__(self):
super().__init__()
self.layout = BoxLayout()
button = Button()
button.bind(on_press=self.remove_layout)
self.layout.add_widget(button)
self.add_widget(self.layout)
def remove_layout(self, *ignore):
self.remove_widget(self.layout)
以下是一种方法:
class Main(Widget):
def __init__(self):
super().__init__()
self.layout = BoxLayout()
button = Button()
button.bind(on_press=self.remove_layout)
self.layout.add_widget(button)
self.add_widget(self.layout)
def remove_layout(self, *ignore):
self.remove_widget(self.layout)
如何使用子按钮删除父BoxLayout?请记住,我将有许多类似的盒子布局
你可以这样做:
on_press:self.parent.parent.remove_widget(self.parent).
所以按钮基本上是告诉它的祖父和它的父亲断绝关系
使用您提供的代码,您可以编写如下代码:
remButton.bind(on_press=self.ids.removeID.parent.parent.remove_widget(self.ids.removeID.parent))
稍微干净一点:
Box = remButton.parent
remButton.bind(on_press=Box.parent.remove_widget(Box))
如何使用子按钮删除父BoxLayout?请记住,我将有许多类似的盒子布局
你可以这样做:
on_press:self.parent.parent.remove_widget(self.parent).
所以按钮基本上是告诉它的祖父和它的父亲断绝关系
使用您提供的代码,您可以编写如下代码:
remButton.bind(on_press=self.ids.removeID.parent.parent.remove_widget(self.ids.removeID.parent))
稍微干净一点:
Box = remButton.parent
remButton.bind(on_press=Box.parent.remove_widget(Box))
我想使用BoxLayout中的remove button小部件删除BoxLayout我想使用BoxLayoutPerfect中的remove button小部件删除BoxLayout!!有道理!
*ignore
参数在remove\u layout
中做什么?该参数实际上是您绑定回调的对象。在这种情况下,它是按钮
。我只是忽略了以这种方式出现的任何参数。太好了!!有道理!*ignore
参数在remove\u layout
中做什么?该参数实际上是您绑定回调的对象。在这种情况下,它是按钮
。我只是忽略了以这种方式出现的任何参数。