Search code examples
pythoncanvaskivytextures

Dynamically change texture of Ellipse in kivy, python


I want to add a texture to Ellipse from python code.

.kv file:

    <User>:
        user_name: username
        user_pic: userpic
        size_hint_y: None
        height: '40dp'
        orientation: 'horizontal'
        padding: 2
        BoxLayout:
            id: userpic
            size_hint_x: 0.2
            canvas.before:
                Ellipse:
                    group: 'pic'
                    pos: self.pos
                    size: self.size

python code:
    class User(BoxLayout):
        user_name = ObjectProperty(None)
        def init(self, texture, name):
            self.ids.user_pic.canvas.get_group['pic'][0].texture = texture
            self.user_name.text = name

some portion of the other code:
user = User()
    img = np.array(json.loads(user_dtls[i][1])).reshape(56, 56, 3)
                img = img.astype(np.uint8)
                img = cv2.flip(img, 0)
                texture = Texture.create(size=(56, 56))
                texture.blit_buffer(img.tostring(), colorfmt='bgr', bufferfmt='ubyte')
                user.init(texture, 'demo')
                self.uh.add_widget(user)

getting following error:

self.ids.user_pic.canvas.get_group['pic'][0].texture = texture

File "kivy\properties.pyx", line 841, in kivy.properties.ObservableDict.getattr AttributeError: 'super' object has no attribute 'getattr'


Solution

  • So, in spite of not having a minimal reproducible example, here is what I think are your main problems:

    • using userpic in one place and user_pic in another
    • get_group is a method, not a list, so it should appear as get_group('pic')
    • your pic group is being created inside the before group (you are using canvas.before), so canvas.get_group should be canvas.before.get_group