python : How to remove widget in kivy

标签 python python-2.7 kivy

我有两个文件 demo.pydemo.kv
当我在点击菜单后运行 demo.py 然后显示 +Add more 按钮。当我点击 +Add more 按钮然后 三行 添加动态,因为我在那里使用循环。每次添加三行 动态
但是有人能告诉我当我添加新行然后如何删除前三行吗?
每次都应该只显示 3 个新行 并且上一行应该是 delete。我正在使用代码

def add_more(self):
    self.remove_widget(Row())
    for x in range(0, 3):
        self.row_count += 1
        self.add_widget(Row(button_text=str(self.row_count)))

演示.py

from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import BooleanProperty, ListProperty, StringProperty, ObjectProperty, NumericProperty
from kivy.uix.popup import Popup

Window.clearcolor = (0.5, 0.5, 0.5, 1)
Window.size = (500, 400)

class User(Popup):
    total_value = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(User, self).__init__(**kwargs)

    def add_more(self):
        self.ids.rows.add_more()


class Row(BoxLayout):
    col_data = ListProperty(["?", "?", "?", "?", "?"])
    name = ObjectProperty(None)
    button_text = StringProperty("")
    col_data3 = StringProperty("")
    col_data4 = StringProperty("")

    def __init__(self, **kwargs):
        super(Row, self).__init__(**kwargs)


class Rows(BoxLayout):
    row_count = 0

    def __init__(self, **kwargs):
        super(Rows, self).__init__(**kwargs)
        self.add_more()

    def add_more(self):
        self.remove_widget(Row())
        for x in range(0, 3):
            self.row_count += 1
            self.add_widget(Row(button_text=str(self.row_count)))


class rv(BoxLayout):
    data_items = ListProperty([])
    mode = StringProperty("")

    def __init__(self, **kwargs):
        super(rv, self).__init__(**kwargs)


    def add(self):
        self.mode = "Add"
        popup = User()
        popup.open()


class MainMenu(BoxLayout):
    content_area = ObjectProperty()

    def display(self):
        self.rv = rv()
        self.content_area.add_widget(self.rv)

class demo(App):

    def build(self):
        return MainMenu()


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

演示.kv

<Row>:
    size_hint_y: None
    height: self.minimum_height
    height: 40

    Button:
        text: root.button_text
        size_hint_x: None
        top: 200

    TextInput:
        id : name
        text: root.col_data3
        width: 300
    TextInput:
        id: number_input
        text: root.col_data4
        width: 300
        input_filter: 'int'


<Rows>:
    size_hint_y: None
    height: self.minimum_height
    orientation: "vertical"

<User>:
    id: user
    BoxLayout:
        orientation: "vertical"
        padding : 20, 5


        BoxLayout:
            orientation: "horizontal"
            #padding : 10, 10
            spacing: 10, 10
            size: 450, 40
            size_hint: None, None

            Label:
                size_hint_x: .2
                text: "Number"
                text_size: self.size
                valign: 'bottom'
                halign: 'center'

            Label:
                size_hint_x: .4
                text: "name"
                text_size: self.size
                valign: 'bottom'
                halign: 'center'

            Label:
                size_hint_x: .4
                text: "Value"
                text_size: self.size
                valign: 'bottom'
                halign: 'center'

        ScrollView:
            Rows:
                id: rows


        BoxLayout:
            orientation: "horizontal"
            size_hint_x: .2
            size_hint_y: .2

            Button:
                text: "+Add More"
                on_press: root.add_more()

<rv>:
    BoxLayout:
        orientation: "vertical"

        Button:
            size_hint: .25, .03
            text: "+Add"
            on_press: root.add()

        GridLayout:
            size_hint: 1, None
            size_hint_y: None
            height: 25
            cols: 3

        BoxLayout:
            orientation: "vertical"


<MenuButton@Button>:
    text_size: self.size
    valign: "middle"
    padding_x: 5
    size : (100, 40)
    size_hint : (None, None)
    background_color: 90 , 90, 90, 90
    background_normal: ''
    color: 0, 0.517, 0.705, 1
    border: (0, 10, 0, 0)

<MainMenu>:
    content_area: content_area

    BoxLayout:
        orientation: 'vertical'
        spacing : 10

        BoxLayout:
            canvas.before:
                Rectangle:
                    pos: self.pos
                    size: self.size

            size_hint_y: 2

            MenuButton:
                text: 'Menu'
                size : (50, 12)
                on_release: root.display()

        BoxLayout:
            id: content_area
            size_hint_y: 30

最佳答案

remove_widget 必须接收要删除的子部件实例作为参数。由于您可以使用其 children 属性获取小部件的子项,因此要删除前三行,您可以执行以下操作:

    def add_more(self):
        if self.children:
            for child in self.children[:3]:
                self.remove_widget(child)

        for x in range(0, 3):
            self.row_count += 1
            self.add_widget(Row(button_text=str(self.row_count)))

不过,使用clear_widgets方法更简单:

def add_more(self):
    self.clear_widgets(self.children[:3])
    for x in range(0, 3):
        self.row_count += 1
        self.add_widget(Row(button_text=str(self.row_count)))

由于您实际上删除了 BoxLayout 中的所有行,您可以:

    def add_more(self):
        self.clear_widgets()
        for x in range(0, 3):
            self.row_count += 1
            self.add_widget(Row(button_text=str(self.row_count)))

编辑:

要重置索引,只需忽略 self.row_count 并使用范围返回的值:

    def add_more(self):
        self.clear_widgets()
        for x in range(1, 4):
            self.add_widget(Row(button_text=str(x)))

关于 python : How to remove widget in kivy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48580686/

相关文章:

python - 在 python 中的单个表达式中打印 int 和 string

python - 导入错误 : cannot import name celery

python - 如何使用 Bottle-python 让 API 接受 URL 作为 GET 或 POST 请求的参数

python - Unicode 字符在 kivy python 中不起作用

python - 无法将 .kv 文件中的对象连接到 python 类

python - 如何在函数中动态生成子类?

python - SQLalchemy 每个类别的前 3 个结果

python - 有没有办法使用 Python-C API 调整 python 列表的大小?

python - 使用 python multiprocessing 运行带有不同参数组合的循环脚本

Python Kivy 严重文本错误。无法找到任何有值(value)的文本提供者