使用Kivy,在按下按钮时动态添加项目 [英] Using Kivy, dynamically add items when a button is pressed

查看:60
本文介绍了使用Kivy,在按下按钮时动态添加项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下代码. 我希望能够在按下添加项目"按钮时动态添加任意数量的项目.

I wrote the following code. I want to be able to dynamically add as many items as I want when I press the Add Item button.

#-*- coding: utf-8 -*-
from kivy.config import Config
Config.set('graphics', 'width', 300)
Config.set('graphics', 'height', 300)

from kivy.lang import Builder
Builder.load_string("""
<AddItemWidget>:
    BoxLayout:
        size: root.size
        orientation: 'vertical'

        RecycleView:
            size_hint: 1.0,1.0

            BoxLayout:
                orientation: 'vertical'

                Button:
                    id: button1
                    text: "Button1"

                Button:
                    id: addButton
                    text: "Add Item"
                    on_press: root.buttonClicked()
""")

from kivy.app import App
from kivy.uix.widget import Widget

from kivy.properties import StringProperty 

class AddItemWidget(Widget):
    text = StringProperty()

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

    def buttonClicked(self):
        print("add item test")

class TestApp(App):
    def __init__(self, **kwargs):
        super(TestApp, self).__init__(**kwargs)

    def build(self):
        return AddItemWidget()

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

单击按钮时如何添加元素,如下图所示?

How do I get an element to be added when I click on a button, as in the image below?

我希望能够增加100或200,具体取决于我单击按钮的次数.

Instead of just adding one, I want to be able to add 100 or 200 depending on how many times I click the button.

我希望也可以删除我添加的按钮.

I'd like to be able to delete the buttons I've added as well.

推荐答案

您可以使用add_widget()将新的Buttons添加到BoxLayout,并且使用index=1始终将新的Button添加到Add Item Button.为此,您需要引用BoxLayout,这可以通过在kv中添加id来实现.这是执行此操作的代码的修改版本:

You can use add_widget() to add new Buttons to your BoxLayout, and using index=1 always adds the new Button just above the Add Item Button. To do that, you need a reference to the BoxLayout, and that can be done by adding an id to it in your kv. Here is a modified version of your code that does that:

#-*- coding: utf-8 -*-
from kivy.config import Config
from kivy.uix.button import Button

Config.set('graphics', 'width', 300)
Config.set('graphics', 'height', 300)
Config.set('input', 'mouse', 'mouse,multitouch_on_demand')  # eliminate annoying circle drawing on right click

from kivy.lang import Builder
Builder.load_string("""
<AddItemWidget>:
    BoxLayout:
        size: root.size
        orientation: 'vertical'

        RecycleView:
            size_hint: 1.0,1.0

            BoxLayout:
                id: box
                orientation: 'vertical'

                Button:
                    id: button1
                    text: "Button1"

                Button:
                    id: addButton
                    text: "Add Item"
                    on_press: root.buttonClicked()
""")

from kivy.app import App
from kivy.uix.widget import Widget

from kivy.properties import StringProperty

class RemovableButton(Button):
    def on_touch_down(self, touch):
        if touch.button == 'right':
            if self.collide_point(touch.x, touch.y):
                self.parent.remove_widget(self)
                return True
        return super(RemovableButton, self).on_touch_down(touch)


class AddItemWidget(Widget):
    text = StringProperty()

    def __init__(self, **kwargs):
        super(AddItemWidget, self).__init__(**kwargs)
        self.count = 1

    def buttonClicked(self):
        print("add item test")
        self.count += 1
        newButt = RemovableButton(text='Button'+ str(self.count))
        self.ids.box.add_widget(newButt, index=1)


class TestApp(App):
    def __init__(self, **kwargs):
        super(TestApp, self).__init__(**kwargs)

    def build(self):
        return AddItemWidget()

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

我在RemovableButton类中扩展了Button,并编写了一个on_touch_down()方法,右键单击该方法可以删除RemovableButton.通过添加RemovableButtons,删除很容易.您可能想要显示一个Popup,使用户有机会改变自己的想法,以进行删除.

I have extended Button in a RemovableButton class and wrote an on_touch_down() method that removes the RemovableButton on a right click. By adding RemovableButtons, the removal is easy. You may want to show a Popup that gives the user a chance to change his/her mind about doing a removal.

这篇关于使用Kivy,在按下按钮时动态添加项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆