[Python] Image not reloading - kivy python

Discussão em 'Python' iniciado por Stack, Maio 10, 2025.

  1. Stack

    Stack Membro Participativo

    So I'm very new to Kivy, and the point here is to have a random integer cycle through 3 random integers and change images corresponding to the integers, with a clock checking this every second. It does show its being changed, even the image source seems to be changing but I'm not seeing any update, just a white screen (And yes, the images did work before when set manually, without this code). Some good help and explanation would go a long way, thanks!

    main.py


    import random

    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.image import Image
    from kivy.clock import Clock


    def rand_num():
    num = random.randint(1,3)
    return num

    class FaceImage(BoxLayout):
    def img_timer(self):
    Clock.schedule_interval(self.swap_face, 1)

    def run_face(img_name):
    num = rand_num()
    if num == 1:
    img_name = 'smile1.png'
    elif num == 2:
    img_name = 'smile2.png'
    elif num == 3:
    img_name = 'smile3.png'
    return img_name

    def swap_face(self, dt):
    self.image = FaceImage().ids.face
    self.image.source = self.run_face()
    print(self.image.source)
    self.image.reload()


    class ServotApp(App):
    def __init__(self, **kwargs):
    super(ServotApp, self).__init__(**kwargs)
    self.img_obj = FaceImage()
    self.img_obj.img_timer()


    def build(self):
    face_img = FaceImage()
    return face_img


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

    servot.kv


    #:kivy 2.3.1
    <FaceImage>:
    orientation: 'vertical'
    canvas:
    Color:
    rgba: 1, 0.93, 0.75, 1
    Rectangle:
    pos: self.pos
    size: self.size
    Image:
    id: face
    size: self.size

    Continue reading...

Compartilhe esta Página