KIVY:如何清除所有子项的网格布局

Posted

技术标签:

【中文标题】KIVY:如何清除所有子项的网格布局【英文标题】:KIVY: How to clear a Grid Layout of all its children 【发布时间】:2018-04-11 15:00:24 【问题描述】:

我正在尝试删除网格布局中的所有按钮,这些按钮是在按下按钮时动态创建的。我尝试使用 clear_widget() 执行此操作,但这不起作用。现在我为那些动态创建的按钮分配 id 并尝试删除它们,但它也不起作用

Python

def drinksSelect(self,value):  # creating a button by referring the id of the layout in which to create button
    drinkImagePath = 'pepsi': 'drinksPictures/pepsi.png','7up': 'drinksPictures/7up.png'
    if self.root.a_s.l < self.root.a_s.limit: # You know what I mean
        st = 'number'
        img = myImage(source= drinkImagePath[value], size=(200,20), id=st)
        self.root.a_s.ids['place_remaining'].add_widget(img)
        self.root.a_s.l += 1

def checkout(self): #when this fucntion is called, it should clear the gridLayout with id drinksLayout
    #self.root.a_s.ids.drinksLayout.clear_widget(self.root.a_s.ids.place_remaining)
    st = 'number'
    self.root.a_s.ids.place_remaining.remove_widget(self.root.a_s.ids.st)

千伏

 GridLayout:
        id: drinksLayout
        size_hint_y: 0.3
        orientation: 'horizontal'
        rows: 1
        GridLayout:
            id: place_remaining
            rows: 1
            size_hint_x: 80
        Button:
            id: label1
            width: 200 
            size_hint: None,0.4
            background_normal:'1.jpg'
            text: 'Checkout'
            on_release: app.checkout()

【问题讨论】:

【参考方案1】:

要清除GridLayout的所有子元素,请参考示例。

示例

main.py

​​>
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.uix.image import Image


class RemoveWidgetDemo(BoxLayout):
    place_remaining = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(RemoveWidgetDemo, self).__init__(**kwargs)
        self.drinksSelect()

    def drinksSelect(self):
        drinkImagePath = 'pepsi': 'drinksPictures/pepsi_logo.png', '7up': 'drinksPictures/7up-logo.png'
        for value in drinkImagePath.values():
            self.place_remaining.add_widget(Image(source=value, size=(200, 20)))

    def checkout(self):
        for child in [child for child in self.place_remaining.children]:
            self.place_remaining.remove_widget(child)


class TestApp(App):
    title = "Kivy Clear GridLayout of all its children"

    def build(self):
        return RemoveWidgetDemo()


if __name__ == '__main__':
    TestApp().run()

test.kv

#:kivy 1.10.0

<RemoveWidgetDemo>:
    place_remaining: place_remaining

    GridLayout:
        id: drinksLayout
        size_hint_y: 0.3
        orientation: 'horizontal'
        rows: 1

        GridLayout:
            id: place_remaining
            rows: 1
            size_hint_x: 80

        Button:
            id: label1
            width: 200
            size_hint: None,0.4
            background_normal:'1.png'
            text: 'Checkout'
            color: 0, 0, 0, 1
            on_release: root.checkout()

输出

【讨论】:

【参考方案2】:

您只需稍微编辑您的 checkout 方法

    def checkout(self):  # when this fucntion is called, it should clear the gridLayout with id drinksLayout
        self.root.a_s.ids.place_remaining.clear_widgets()
        #but don't forget
        self.root.a_s.l = 0

【讨论】:

以上是关于KIVY:如何清除所有子项的网格布局的主要内容,如果未能解决你的问题,请参考以下文章

Kivy 网格布局固定了某些列的列宽

如何将纯 python 中动态创建的按钮添加到用 Kivy 语言编写的 kivy 布局中?

CSS网格布局(Grid)教程

如何使用 CustomScrollView 功能布局子网格的动态宽度?

如何在Kivy(Python)中叠加2个布局?

iphone中的所有应用程序都以“网格图标”布局出现,我该如何实现这种布局?