删除kivy点击图片

时间:2015-04-15 20:49:53

标签: python-3.x kivy

我正在尝试用kivy做一个基本的隐藏对象游戏,但是因为我是一个新手,所以有点难。所以我所做的是使用flowLayout的画布我已经显示了4张图像,现在我希望它们在我点击 ON THEM 后消失。我应该怎么做以及实现这个目标的最佳方法是什么?

另外,我想要一个标签来显示倒计时。我放了一个标签,但它不起作用。所以我暂时只是评论它。如果有人可以解释我哪里出错了。

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):
    time_number = StringProperty()

    def __init__(self, **kwargs):
        super(MyWidget, self).__init__(**kwargs)
        self.time_number = str(50)
    def call(self):
        if running:
            #print(self.time_number)
            #self.time_number = str(int(self.time_number)+1)
            pass            
    def clicked(self):
        global running
        #self.time_number = 50
        running=False

    Clock.schedule_interval(call, 1)

    pos1 =(0) #random.randint(-200,200)
    pos2 =(0) #random.randint(-200,200)

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


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

.kv文件

<MyWidget>
    AnchorLayout:
        BoxLayout:           
            Image:
                source:'B_image.png'
        BoxLayout:

            Label:
                text:root.time_number
        FloatLayout:
            canvas:
                Rectangle:
                    pos:(root.pos1,root.pos2)
                    size: 24, 24
                    source:'image.png'
                Rectangle:
                    pos:(root.pos1 + 100,root.pos2)
                    size: 24, 24
                    source:'image.png'
                Rectangle:
                    pos:(root.pos1,root.pos2 + 100)
                    size: 24, 24
                    source:'image.png'
                Rectangle:
                    pos:(root.pos1 + 100,root.pos2 + 100)
                    size: 24, 24
                    source:'image.png'

1 个答案:

答案 0 :(得分:1)

对于每个矩形,将on_press属性设置为从其父窗口小部件中删除矩形窗口小部件的函数。如:

<MyWidget>:
    grid_layout: grid_layout
    AnchorLayout:
        BoxLayout:
            Image:
                source:'B_image.png'
        BoxLayout:
            Label:
                text:root.time_number
        GridLayout:
            rows: 2
            id: grid_layout
            Button:
                background_normal: 'image.png'
                on_press: root.remove_rectangle(widget=self)
            Button:
                background_normal: 'image.png'
                on_press: root.remove_rectangle(widget=self)
            Button:
                background_normal: 'image.png'
                on_press: root.remove_rectangle(widget=self)
            Button:
                background_normal: 'image.png'
                on_press: root.remove_rectangle(widget=self)

在main.py中:

class MyWidget(AnchorLayout):
    ...
    def remove_rectangle(self, widget):
        self.grid_layout.remove_widget(widget)