如何在 kivy 中删除图像后重新添加图像

How to Add images back after removing them in kivy

我试图在 UI 上重新添加图像,一旦它们全部被删除,我尝试执行 add_widget 但它返回了这个错误。我只想在删除前 10 张图片后再添加 10 张图片。

错误信息

<   File "/usr/lib/python2.7/dist-packages/kivy/lang.py", line 1465, in custom_callback
     exec(__kvlang__.co_value, idmap)
   File "/home/nabeel/Desktop/widgets.kv", line 25, in <module>
     on_press: root.remove_rectangle(widget=self)
   File "/home/nabeel/Desktop/widgets.py", line 44, in remove_rectangle
     self.grid_layout.add_widget(widget)
   File "/usr/lib/python2.7/dist-packages/kivy/uix/floatlayout.py", line 116, in add_widget
     return super(FloatLayout, self).add_widget(widget, index)
   File "/usr/lib/python2.7/dist-packages/kivy/uix/layout.py", line 81, in add_widget
     return super(Layout, self).add_widget(widget, index)
   File "/usr/lib/python2.7/dist-packages/kivy/uix/widget.py", line 453, in add_widget
     % (widget, parent))
 kivy.uix.widget.WidgetException: Cannot add <kivy.factory.ImgButton object at 0x7f580b454258>, it already has a parent <kivy.uix.floatlayout.FloatLayout object at 0x7f580b44f4c8>
>

.py

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.image import Image
from kivy.config import Config
from kivy.clock import Clock
from kivy.properties import StringProperty
from kivy.graphics.instructions import InstructionGroup
from kivy.graphics.context_instructions import Color

import random

Config.set('graphics', 'width', '480')
Config.set('graphics', 'height', '320')


running = True


class MyWidget(AnchorLayout):

    LOC = []
    for i in range (10):
        LOC.append((random.randint(0,400),random.randint(0,300)))

    num = 9

    b_G_IMG = StringProperty("B_image.png")
    time = 40
    time_number = StringProperty()

    def __init__(self, **kwargs):
        super(MyWidget, self).__init__(**kwargs)
        self.time_number = str(self.time)

    def remove_rectangle(self, widget):
        if self.num > 1:
            self.grid_layout.remove_widget(widget)
            self.set_level()
            self.num -= 1
        else:
            self.grid_layout.add_widget(widget)
            self.num = 9



    def call(self):
        if True:
            self.time += 1
            print(self.time)
            self.time_number = str(self.time)           
    def clicked(self):
        #global running
        #self.time_number = 50
        running=False

    Clock.schedule_interval(call, 1)

class WidgetsApp(App):
    def build(self):
        return MyWidget()


if __name__=="__main__":
    WidgetsApp().run()

.kv 文件

<ImgButton@Button>:

    size_hint:(None,None)
    size:(60,60)

<MyWidget>:
    grid_layout: grid_layout
    AnchorLayout:
        BoxLayout:
            Image:
                source:root.b_G_IMG
        BoxLayout:
            Label:
                text:root.time_number
        FloatLayout:

            id: grid_layout

            ImgButton:
                pos:(root.LOC[0])
                background_normal: 'image.png'
                on_press: root.remove_rectangle(widget=self)
            ImgButton:
                pos:(root.LOC[1])
                background_normal: 'image.png'
                on_press: root.remove_rectangle(widget=self)
            ImgButton:
                pos:(root.LOC[2])
                background_normal: 'image.png'
                on_press: root.remove_rectangle(widget=self)    
            ImgButton:
                pos:(root.LOC[3])
                background_normal: 'image.png'
                on_press: root.remove_rectangle(widget=self)
            ImgButton:
                pos:(root.LOC[4])
                background_normal: 'image.png'
                on_press: root.remove_rectangle(widget=self)
            ImgButton:
                pos:(root.LOC[5])                   
                background_normal: 'image.png'
                on_press: root.remove_rectangle(widget=self)
            ImgButton:
                pos:(root.LOC[6])
                background_normal: 'image.png'
                on_press: root.remove_rectangle(widget=self)
            ImgButton:
                pos:(root.LOC[7])
                background_normal: 'image.png'    
                on_press: root.remove_rectangle(widget=self)
            ImgButton:
                pos:(root.LOC[8])
                background_normal: 'image.png'
                on_press: root.remove_rectangle(widget=self)
            ImgButton:
                pos:(root.LOC[9])
                background_normal: 'image.png'
                on_press: root.remove_rectangle(widget=self)

与其尝试添加回调用 remove_rectangle 的同一个小部件,不如在 remove_rectangle 中创建一个新的小部件。做一些像

class ImgButton(Button):
    pass



def remove_rectangle(self, widget):
    ...
    else:
        new_image = ImgButton(background_normal='image.png')

        self.grid_layout.add_widget(new_image)