删除猕猴桃中的点击图片 [英] removing Images on clicks in kivy
问题描述
我正尝试用基维制作一个基本的隐藏对象游戏,但是由于我是新手,所以有点困难.因此,我要做的是使用flowLayout画布显示了4张图像,现在我希望它们在单击 ON THEM 后消失.我应该怎么做,什么是实现这一目标的最佳方法?
I am Trying to make a basic Hidden Object Game with kivy, But since i am a novice it is a little hard. So What I have done is using the canvas of flowLayout I have displayed 4 images, now I want them to disappear once I click ON THEM. how should I do this and what is the best way to achieve this???
还有另一件事,我希望标签显示倒计时.我放置了一个标签,但是它不起作用.所以我暂时将其注释掉.如果有人可以请解释我要去哪里错了.
Also another thing, I want a label to display a countdown. I have placed a Label But It isn't working. SO I just commented out it for the time being. If some one could please explain where I am going wrong.
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'
推荐答案
对于每个矩形,将on_press属性设置为一个函数,该函数将从其父窗口小部件中删除该矩形窗口小部件.如:
For each rectangle, set an on_press attribute to a function that removes the rectangle widget from its parent widget. Such as:
<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)
这篇关于删除猕猴桃中的点击图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!