Skip to main content
 首页 » 编程设计

python之为什么以及由谁在 kivy 中调用这种方法

2025年05月04日8cloudgamer

我的第一步是使用 kivy 并尝试绘制通过 matplotlib 或 pygal 在 TextInput 中输入的函数。只要我将图像保存在磁盘上,两者都可以正常工作。为了加快程序速度,我不想将图像写入磁盘或从磁盘读取图像。我在 mornie.org 上找到了以下解决方案:

class MemoryImage(Image): 
    """Display an image already loaded in memory.""" 
    memory_data = ObjectProperty(None) 
 
    def __init__(self, memory_data, **kwargs): 
            super(MemoryImage, self).__init__(**kwargs) 
 
            self.memory_data = memory_data 
 
    def on_memory_data(self, *args): 
            """Load image from memory.""" 
            data = StringIO.StringIO(self.memory_data) 
            with self.canvas: 
                    self.texture = ImageLoaderPygame(data).texture 

通过随机更改单词直到它在 python3 下运行我找到了这个解决方案:

class MemoryImage(Image): 
    """Quelle: https://mornie.org/blog/2013/11/06/how-load-image-memory-kivy/""" 
    memory_data = ObjectProperty(None) 
    def __init__(self,memory_data,**kwargs): 
            """Display an image already loaded in memory.""" 
            super(MemoryImage, self).__init__(**kwargs) 
            self.memory_data = memory_data 
 
    def on_memory_data(self, *args): 
            """Load image from memory.""" 
            data = BytesIO(self.memory_data) 
            with self.canvas: 
                    self.texture = ImageLoaderPygame(filename="test.png",rawdata=data, ext="png", inline=True).texture 

只要我不尝试更新图像,此解决方案就可以正常工作。不幸的是,这是我的意图。所以我试着去理解这个类。但是我不能——在前面我是 OOP 的新兵——找到一个调用这个 on_memory_data 方法的点。首先我搜索了这个类的父类,然后我搜索了 kivy,最后搜索了我的整个/usr 文件夹以查找包含“on_memory”的 python 文件,但什么也没有。

如果有人能给我提示,为什么要调用这个方法,我将不胜感激!

请您参考如下方法:

on_memory_data 在 memory_data ObjectProperty 更改时调用(即,当它引用的对象被替换时,它不知道对象的内部更改)。这是 kivy 属性的自动行为。