简体   繁体   English

Kivy:每次触摸后(on_touch_down)在FloatLayout中添加图像,但以前的所有图像都消失了

[英]Kivy : adding Images in FloatLayout after each touch (on_touch_down), but all previous images disappeared

I am trying to plot an image ( Image object) each time the on_touch_down method is triggered. 我每次on_touch_down方法被触发时都试图绘制图像( Image对象)。 Each image has the same source. 每个图像都有相同的来源。 This is done inside the FloatLayout and the plotting is done by self.add_widget(theImage) . 这是在FloatLayout内部完成的,并且绘制是通过self.add_widget(theImage) I got no error, but can only display one image after each touch (the previous images disappeared after each touch). 我没有出错,但是每次触摸后只能显示一张图像(以前的图像在每次触摸后都会消失)。

But if i write print(self.children) inside the on_touch_down , we can see that the list is updated at every touch . 但是如果我在on_touch_down里面写print(self.children) ,我们可以看到列表在每次touch时都会更新。

Why is this? 为什么是这样? Thanks. 谢谢。

(*The image size is such that the screen can fit 20 images without colliding each other) (*图像大小使得屏幕可以容纳20张图像而不会彼此碰撞)

The code : 编码 :

from kivy.app import App
from kivy.uix.image import Image
from kivy.uix.floatlayout import FloatLayout
import random

class Screen(FloatLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
    def on_touch_down(self, touch):           
        super().on_touch_down(touch)
        plot = Image(source='bottle.jpg')
        plot.pos_hint['x'] = random.uniform(0,1) - 0.5*plot.size_hint_x
        plot.pos_hint['y'] = random.uniform(0,1) - 0.5*plot.size_hint_y
        self.add_widget(plot)

class MyApp(App):
    def build(self):
        return Screen()

app = MyApp()

if __name__ == '__main__':
    app.run()

According to what is observed if a pos_hint is not established when the widget is created, it has the value of an empty dictionary that is a property of the class and not of the object, meaning that if a value is established an object is established to the others objects. 根据观察到的结果,如果在创建窗口小部件时未建立pos_hint,则它具有空字典的值,该字典是类的属性,而不是对象的属性,这意味着如果建立了值,则将建立一个对象以其他对象。

To verify this, the id of pos_hint is printed obtaining the same value: 为了验证这一点,将打印pos_hint的id以获得相同的值:

def on_touch_down(self, touch):           
    super().on_touch_down(touch)
    plot = Image(source='bottle.jpg')
    plot.pos_hint['x'] = random.uniform(0,1) - 0.5*plot.size_hint_x
    plot.pos_hint['y'] = random.uniform(0,1) - 0.5*plot.size_hint_y
    print(id(plot.pos_hint))
    self.add_widget(plot)

Output: 输出:

140252781257784
140252781257784
140252781257784
140252781257784
...

So the solution is to pass a new dictionary: 因此解决方案是传递新字典:

def on_touch_down(self, touch):           
    super().on_touch_down(touch)
    plot = Image(source='bottle.jpg', pos_hint={'x':.0, 'y':.0}) # <-- default value
    plot.pos_hint['x'] = random.uniform(0,1) - 0.5*plot.size_hint_x
    plot.pos_hint['y'] = random.uniform(0,1) - 0.5*plot.size_hint_y
    self.add_widget(plot)

Or pass directly the new dictionary: 或直接通过新词典:

def on_touch_down(self, touch):           
    super().on_touch_down(touch)
    plot = Image(source='bottle.jpg')
    plot.pos_hint = {'x': random.uniform(0,1) - 0.5*plot.size_hint_x, 'y': random.uniform(0,1) - 0.5*plot.size_hint_y}
    self.add_widget(plot)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM