如何引用似乎超出范围的widget-adapted-kivy/examples/demo/images

2024-04-20 15:10:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我编辑了kivy/examples/demo/images文件,尝试执行以下操作: -对于每个添加的图像,它还会在侧边栏中添加一个按钮(不同的boxlayout) -单击按钮时,会同时删除图像和按钮

为了做到这一点,我创建了(这是一个简短的摘要代码,给你一个想法)

boxlayout:
    floatlayout:
    #this is where the images, in their own class called "Picture" just like in the demo get added
    boxlayout:
    #this is where the buttons get added. I created a class for them called "PictureBtn"

当创建图片时,我会给它们添加一个唯一的id。 然后创建PictureBtn并添加相同的id。 然后我将图片和按钮填充到各自的位置。到目前为止一切都很好。在

现在的问题是,从Pictures和PictureBtn实例中,我都可以引用该应用程序,但我不知道如何引用另一个实例。身份证好像不在任何地方。在

我的理解是因为它们超出了范围。id只能在本地保存,因为PictureBtn和Pictures都有自己的根,所以我无法访问它们。在

我的完整代码如下:

^{pr2}$

那么我的kv文件包含以下内容:

#:kivy 1.0
#:import kivy kivy
#:import win kivy.core.window

BoxLayout:
    orientation: 'horizontal'
    FloatLayout:
        id: picspace
        size_hint: 0.8,1
        canvas:
            Color:
                rgb: 1, 1, 1
            Rectangle:
                source: 'data/images/background.jpg'
                size: self.size

        BoxLayout:
            padding: 10
            spacing: 10
            size_hint: 1, None
            pos_hint: {'top': 1}
            height: 44
            Image:
                size_hint: None, None
                size: 24, 24
                source: 'data/logo/kivy-icon-24.png'
            Label:
                height: 24
                text_size: self.width, None
                color: (1, 1, 1, .8)
                text: 'Kivy %s - Pictures' % kivy.__version__
    BoxLayout:
        id: sidebar
        orientation: 'vertical'


<Picture>:
    # each time a picture is created, the image can delay the loading
    # as soon as the image is loaded, ensure that the center is changed
    # to the center of the screen.
    on_size: self.center = win.Window.center
    size: image.size
    size_hint: None, None

    Image:
        id: image
        source: root.source

        # create initial image to be 400 pixels width
        size: 400, 400 / self.image_ratio

        # add shadow background
        canvas.before:
            Color:
                rgba: 1,1,1,1
            BorderImage:
                source: 'shadow32.png'
                border: (36,36,36,36)
                size:(self.width+72, self.height+72)
                pos: (-36,-36)

<PictureBtn>:
    orientation: 'horizontal'
    Label:
        id: lbl
        text: ''
    Button:
        text: 'X'
        on_release: app.RemovePicture(self.parent.id)

(我的原始代码也是类似的情况,但我在kivy/示例中创建了相同的问题,以使其更易于解决。)

谢谢


Tags: thetextimageselfnoneidsourcesize
2条回答

好吧,我终于找到了一个办法,但我确信一定有更好的办法:

def RemovePicture(self,idee):
    for child in self.ids.picspace.children:
        if child.id==idee:
            self.ids.picspace.remove_widget(child)   
    for child in self.ids.sidebar.children:
        if child.id==idee:
            self.ids.sidebar.remove_widget(child)   

所以这似乎有效。它们不会出现在ID列表中,但会出现在子列表中。我还是不知道为什么会这样。如果有人知道更好/更合适的方法,请告诉我。在

ids仅在kv中有效,并且与根规则相关。它们不是用于引用小部件的应用程序范围的全局标识符。下面是一个简单的例子,为什么会这样:

<MyWidget@BoxLayout>:
    Label:
        id: mylabel
    Button:
        id: mybutton

BoxLayout:
    MyWidget:
        id: widget1
    MyWidget:
        id: widget2

在这个例子中,id mylabel将引用哪个小部件?widget1中的实例还是{}中的实例?在

因此,您可以遍历子对象来查找和删除小部件,也可以将引用存储在dict中。例如,在PicturesApp类上创建一个_picturesdict,然后在添加图片时:

^{pr2}$

你的删除代码会变成:

def remove_picture(self, idee):
    picture, button = self._pictures.pop(idee)
    picture.parent.remove_widget(picture)
    button.parent.remove_widget(button)

相关问题 更多 >