如何在kivy中删除它们后添加图像

时间:2015-04-19 04:23:24

标签: kivy python-2.x

我试图在UI全部删除后添加回来的图像,我尝试做add_widget,但它回退了这个错误。我只想在删除前十个图像后再添加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)

1 个答案:

答案 0 :(得分:0)

您需要在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)